Java HTMLUnit MalformedURLException

时间:2009-12-22 16:05:52

标签: java

我正在尝试从HTMLUnit tutorial

执行此代码
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");

但是在Eclipse中保存它时,我在第二行得到MalformedURLException(如果我编译并运行代码,我也得到它)。有什么问题? TIA

PS:我是Java的新手

向上:

这是堆栈跟踪:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/auth/CredentialsProvider
    at Tester.main(Tester.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 1 more

3 个答案:

答案 0 :(得分:2)

我在Eclipse 3.5中尝试了这个并且它正常工作,测试正在通过。我假设您已将必要的HTMLUnit JAR包含到您的项目中?我从HTMLUnit lib目录中获取了所有JAR,并将它们添加到我项目的构建路径中。

另外,你可以捕获异常并在此处发布堆栈跟踪吗?

try {
  final WebClient webClient = new WebClient();
  final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
  assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
}
catch (Exception e) {
  e.printStackTrace();
}

答案 1 :(得分:0)

package com.project.test;

import java.io.IOException;
import java.net.MalformedURLException;

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



public class Practice1 {

    public static void main(String[] args) {

         final WebClient webClient = new WebClient();
            HtmlPage page;
            try {
                page =  (HtmlPage) webClient.getPage("http://htmlunit.sourceforge.net");
                 System.out.println("Title="+ page.getTitleText());
            } catch (FailingHttpStatusCodeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }




    }
}

答案 2 :(得分:0)

因为在你的堆栈跟踪中你有

Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider

这不是格式错误的网址例外。

作为confirmed by JARFinder,org.apache.commons.httpclient.auth.CredentialsProvider类应该来自commons-httpclient-3。*。jar。因此,问题的原因必定是您的类路径中没有Commons HTTPClient 3.x JAR。