简体   繁体   English

HtmlUnit 第一步

[英]HtmlUnit first steps

I am trying to set up my first example program with HtmlUnit.我正在尝试使用 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 javac -cp lib/htmlunit-2.9.jar 测试.java

but when i try to exec test i get但是当我尝试执行测试时,我得到

java -cp lib/htmlunit-2.9.jar test java-cp lib/htmlunit-2.9.jar 测试

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.线程“main”中的异常 java.lang.NoClassDefFoundError: test Caused by: java.lang.ClassNotFoundException: test at java.net.URLClassLoader$1.run(URLClassLoader.java:217) 在 java.security.doPcessingMethod(security.doPcessingMethod) ) 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) 找不到主 class: 测试。 Program will exit.程序将会退出。

where is the problem?问题出在哪里? i lack some other packages?我缺少其他一些包裹?

you may have to add the current path to the classpath.您可能必须将当前路径添加到类路径中。

On Unix/Linux/Mac:在 Unix/Linux/Mac 上:

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

On Windows:在 Windows 上:

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

EDIT There is actually more jars needed for htmlunit that just htmlunit-2.9.jar.编辑htmlunit 实际上需要更多的 jars 而不仅仅是 htmlunit-2.9.jar。 Therefore, considering that all those required jars are located in the lib/ directory, you should actually invoke the following:因此,考虑到所有需要的 jars 都位于 lib/ 目录中,您实际上应该调用以下命令:

On Unix/Linux/Mac:在 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.如果您从 shell 运行此命令,您还需要转义通配符(使用“\”)或将整个类路径放在引号内以避免 shell 扩展。

On Windows:在 Windows 上:

java -cp .;lib/* test

This works:这有效:

java -cp.:lib/* test java -cp.:lib/* 测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM