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 ();
}
}
All you need to do is go to the Window –> Preferences menu as shown. Then for both *.htm and *.html, click on the Eclipse HTML Editor in the Window below and click the Default and OK to apply. The result should be like below. Anyway, that was pretty easy, wasn’t it? Now, let’s start building the sample application! 2. Eclipse HTML Plugin Tutorial
Introduction In this article, we will introduce HtmlUnit, a tool that allows us to, simply put, interact with and test an HTML site programmatically, using JAVA APIs. 2. About HtmlUnit HtmlUnit is a GUI-less browser – a browser intended to be used programmatically and not directly by a user.
Eclipse does not support subprojects. The Eclipse way of life is one or more projects in a workspace (perhaps using work sets to avoid seeing them all). As a consequence we have established an approach where we switch workspaces when switching projects. Team Projects are nice for this for CVS.
In the previous tutorial of this Java Eclipse tutorial series, we have learned to Install Eclipse by downloading the Eclipse IDE (Photon) and setting up a workspace for it. In this tutorial, we will learn all about creating a new Java project in the Eclipse IDE and working with it. #1) Click on File -> New -> Java project.
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
New java project with default settings download library latest version of HTMUnit from Download Latest HTMLUnit jar Select new project properties-> Java Build Path -> go to library tab and add the extracted all jars files. Create a new class with main method within your new project and run a simple appliation and add this method in class and call it in main method.
`@Test
public void getElements() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://some_url");
final HtmlDivision div = page.getHtmlElementById("some_div_id");
final HtmlAnchor anchor = page.getAnchorByName("anchor_name");
webClient.closeAllWindows();
}`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With