1
votes

I am trying to set up my first example program with HtmlUnit. This is the code:

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.WebClient;


public class test {

public static void main(String[] args) throws Exception {

    WebClient client = new WebClient();
    HtmlPage currentPage = client.getPage("http://www.oddsportal.com/matches/soccer");
    client.waitForBackgroundJavaScript(10000);
    String textSource = currentPage.asXml();
    System.out.println(textSource);

}

}

then i compile:

javac -cp lib/htmlunit-2.9.jar test.java

but when i try to exec test i get

java -cp lib/htmlunit-2.9.jar test

Exception in thread "main" java.lang.NoClassDefFoundError: test Caused by: java.lang.ClassNotFoundException: test at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: test. Program will exit.

where is the problem? i lack some other packages?

2

2 Answers

4
votes

you may have to add the current path to the classpath.

On Unix/Linux/Mac:

java -cp .:lib/htmlunit-2.9.jar test

On Windows:

java -cp .;lib/htmlunit-2.9.jar test

EDIT There is actually more jars needed for htmlunit that just htmlunit-2.9.jar. Therefore, considering that all those required jars are located in the lib/ directory, you should actually invoke the following:

On Unix/Linux/Mac:

java -cp .:lib/* test

If you run this command from a shell, you also need to escape the wildcard (with a '\') or put the whole classpath within quotes to avoid shell expansion.

On Windows:

java -cp .;lib/* test
0
votes

This works:

java -cp .:lib/* test