My project includes htmlunit jars and downloads some pages content. Executable jar (which includes libs, funct. of eclipse export) thereof, however, works only on the machine on which I created it (on different it doesn't execute).
EDIT: It doesn't execute as it doesn't show "Starting Headless Browser" MessageBox upon startup. I used Eclipse Indigo: File > Export > Runnable jar > package required libratries into generated jar
Help, gods:
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: further code, as requested
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 ();
}
}
This is how to set up HtmlUnit and how to export it to a runnable jar file in eclipse:
If this worked for a new project then update your own project to reflect the steps taken in the list. Hope this helps