Dự án của tôi bao gồm các lọ htmlunit và tải xuống một số nội dung của trang. Jar thực thi (bao gồm libs, funct. Của eclipse export), tuy nhiên, chỉ hoạt động trên máy mà tôi đã tạo nó (trên nó không thực thi).Làm thế nào để thiết lập HtmlUnit trong một dự án Eclipse?
EDIT: Nó không thực thi vì nó không hiển thị MessageBox "Bắt đầu từ trình duyệt không đầu" khi khởi động. Tôi sử dụng Eclipse Indigo: File> Export> Runnable jar> gói libratries cần vào jar tạo
Help, vị thần:
import java.io.*;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
EDIT: Mã hơn nữa, theo yêu cầu
public class MyTest
{
public static void main(String[] arguments) {
try{
JOptionPane.showMessageDialog(null, "Starting Headless Browser");
JFileChooser fr = new JFileChooser();
FileSystemView fw = fr.getFileSystemView();
String MyDocuments = fw.getDefaultDirectory().toString();
FileInputStream fstream = new FileInputStream(MyDocuments+"\\Links.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String strLineID;
FileWriter xfstream = new FileWriter(MyDocuments+"\\NewPageContentList.txt");
BufferedWriter out = new BufferedWriter(xfstream);
while ((strLineID = br.readLine()) != null) {
strLine = br.readLine();
out.write(strLineID);
out.write("\r\n");
out.write(DownloadPage(strLine));
out.write("\r\n");
}
out.close();
in.close();
JOptionPane.showMessageDialog(null, "HeadLess Browser Process Has Finished");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "error");
}
}
public static String DownloadPage(String str){
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnScriptError(false);
try{
final HtmlPage page = webClient.getPage(str);
final String pageAsText = str_replace("\n","",str_replace("\r","",page.asText()));
return pageAsText;
}
catch(IOException e){
JOptionPane.showMessageDialog(null, "error");
}
webClient.closeAllWindows();
return "";
}
public static String str_replace (String search, String replace, String subject)
{
StringBuffer result = new StringBuffer (subject);
int pos = 0;
while (true)
{
pos = result.indexOf (search, pos);
if (pos != -1)
result.replace (pos, pos + search.length(), replace);
else
break;
}
return result.toString();
}
}
Tại sao bạn cảm thấy nó không hoạt động trên máy khác? Tôi đoán là máy khác không có các lọ cần thiết trong classpath. Đăng lỗi. –
Nếu không có một stacktrace, thêm mã, vv chúng tôi đang có một chút trong bóng tối như thế nào để trả lời câu hỏi của bạn. –
Tôi đã cập nhật Câu hỏi của mình. Vui lòng sửa lại. Tôi không thấy bất kỳ lỗi nào vì không có gì xảy ra khi tôi "doubleclick" tệp trên máy đích. Bạn có đề xuất nhật thực đã không handeled classpath? Tôi không biết stacktrace là gì, ứng dụng của tôi. – John