我想用HtmlUnit設置我的第一個示例程序。這是代碼:HtmlUnit的第一步
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);
}
}
然後我編譯:
javac -cp lib/htmlunit-2.9.jar test.java
但是當我嘗試exec的測試,我得到
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.
問題出在哪裏?我缺少一些其他包?
我曾嘗試在許多方面,這不作品。 – emanuele 2012-04-08 18:24:42
好吧,環顧四周後,我發現你的問題爲什麼沒有被這條線解決。這是因爲你在htmlunit中有不止一個jar。因此,您實際上需要將它們全部列出或在路徑中使用通配符。 – 2012-04-09 08:13:36