How to configure HtmlUnit in an Eclipse project?

My project includes htmlunit jars and loads the contents of some pages. However, the executable jar (which includes libs, funct. Eclipse export) only works on the machine on which I created it (it does not execute in different ways).

EDIT: it does not run because the MessageBox โ€œStart Mute Browserโ€ message is not displayed at startup. I used Eclipse Indigo: File> Export> Runnable jar> package needed in the library, in the 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 (); } } 
+8
java eclipse jar executable htmlunit
source share
3 answers

Here's how to configure HtmlUnit and how to export it to jar executable in eclipse:

  • Create a new java project (all default settings)
  • Right-click on the project (in the package explorer view) and go to New-> Folder and name it "lib"
  • Download htmlUnit Library (htmlunit-2.9-bin.zip file)
  • Open it and copy the contents of the "/htmlunit-2.9/lib/" folder of the uncompressed file to our lib folder (you can drag and drop all files from the / linux desktop window into eclipse explorer and the choice to copy files)
  • Right-click on the project again and go to "Build Path" โ†’ "Configure Build Path".
  • In the Libraries tab, click Add JAR ...
  • Look for our new library folder (if you donโ€™t close it and go back to the package explorer again, select the project folder and press F5 and continue from step 5)
  • Select all the files inside this folder (17 files in HtmlUnit 2.9) and close all windows
  • Check if everything is in order by creating a very simple application (I accidentally wrote simple code in question , which can help you)
  • Everything should be fine (if itโ€™s not, double-check the steps), so give the opportunity to export the application by right-clicking on the project and selecting "Export" ...
  • Locate the Java / Runnable JAR file and click Next.
  • Select the appropriate launch configuration, destination, and select "Package the required libraries in the generated JAR" if you need only one large file containing your application and HtmlUnit, and click "Finish"
  • Open the console where your JAR file is located and execute "java -jar yourJARfile.jar" and enjoy your application

If this worked for a new project, update your own project to reflect the steps taken on the list. Hope this helps

+20
source share

New java project with default settings Download the latest version of HTMUnit from Download the latest HTMLUnit jar Select new project properties โ†’ Java build path โ†’ go to the library tab and add the extracted all jars files. Create a new class with the main method in a new project and run a simple application and add this method to the class and call it in the 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(); }` 
0
source share

I tried the above answers and they did not work for me.

They are needed, but I had a dynamic web project, so I also needed to add all my .jar files to the lib directory in the WEB-INF directory.

eg. ProjectName \ WebContent \ WEB-INF \ lib (all .jar files you use)

0
source share

All Articles