我的项目包括htmlunit jars并下载一些页面内容。然而,可执行jar(其中包括libs,eclipse导出函数)只适用于我创建它的机器上(不同的它不执行)。如何在Eclipse项目中设置HtmlUnit?
编辑:它不执行,因为它启动时不显示“启动无头浏览器”MessageBox。我使用的Eclipse靛蓝:文件>导出>运行的JAR>包需要libratries到生成的JAR
帮助,神:
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;
编辑:进一步代码,请
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();
}
}
2012-02-01
John
+0
为什么你觉得它不能在其他机器上执行?我的猜测是其他机器在classpath中没有所需的jar。发布错误。 –
2012-02-01 13:18:53
+1
没有堆栈跟踪,进一步的代码等,我们有点在黑暗中如何回答你的问题。 –
2012-02-01 13:19:35
+0
我更新了我的问题。请修改。当我“双击”目标机器上的文件时,我没有看到任何错误,因为没有任何反应。你是否建议eclipse没有处理类路径?我不知道堆栈跟踪是什么,我的appologies。 –
2012-02-01 13:38:50