gpt4 book ai didi

java - HtmlUnit 第一步

转载 作者:行者123 更新时间:2023-11-29 03:47:55 25 4
gpt4 key购买 nike

我正在尝试使用 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

但是当我尝试执行测试时,我得到了

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.

问题出在哪里?我还缺少其他一些软件包吗?

最佳答案

您可能必须将当前路径添加到类路径。

在 Unix/Linux/Mac 上:

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

在 Windows 上:

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

编辑htmlunit 实际上需要更多的 jar,而不仅仅是 htmlunit-2.9.jar。因此,考虑到所有需要的 jar 都位于 lib/目录中,您实际上应该调用以下命令:

在 Unix/Linux/Mac 上:

java -cp .:lib/* test

如果您从 shell 运行此命令,您还需要转义通配符(使用“\”)或将整个类路径放在引号内以避免 shell 扩展。

在 Windows 上:

java -cp .;lib/* test

关于java - HtmlUnit 第一步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10064488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com