2012-04-08 62 views
1

我想用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.

問題出在哪裏?我缺少一些其他包?

回答

0

這工作:

java -cp .:lib/* test

4

您可能必須將當前路徑添加到類路徑。

在Unix/Linux/Mac的:

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

在Windows上:

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

編輯 有需要的,僅僅的HtmlUnit-2.9.jar實際的HtmlUnit多瓶。因此,考慮到所有這些必須的jar位於lib /目錄下,你實際上應該調用以下:

在Unix/Linux/Mac的:

java -cp .:lib/* test 

如果從shell中運行此命令,您還需要轉義通配符(使用'\')或將整個類路徑放在引號內以避免擴展。

在Windows上:

java -cp .;lib/* test 
+0

我曾嘗試在許多方面,這不作品。 – emanuele 2012-04-08 18:24:42

+0

好吧,環顧四周後,我發現你的問題爲什麼沒有被這條線解決。這是因爲你在htmlunit中有不止一個jar。因此,您實際上需要將它們全部列出或在路徑中使用通配符。 – 2012-04-09 08:13:36