开发者

Apache HttpClient throws a NoClassDefFoundError

I have: ubuntu-9.10-desktop-amd64 + NetBeans6.7.1

  1. I just downloaded "Commons HttpClient 3.1 (legacy)" from http://hc.apache.org/downloads.cgi .
  2. I created new application with IDE NetBeans.
  3. I focused on Projects -> Libraries -> Add JAR/Folder and here added commons-httpclient-3.1.jar
  4. By now, I can write "import org.apache.commons.httpclient.*;" This is OK.
  5. But, if I try to add any statement included something like this:

    public static void main(String[] args) {
        HttpClient client = new HttpClient();
    }
    
  6. It is compiled well.

  7. But when I run this project, I receive the following errors:

    ***Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66)
        at SimplestAppl.Main.main(Main.java:22)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
        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开发者_Python百科.ClassLoader.loadClass(ClassLoader.java:319)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
        ... 2 more
    Java Result: 1***
    

What is wrong?

I tried to download "HttpClient 4.0 (GA)". But the result was same.


What you are experiencing is just a missing dependency. Java developers are used to gather their dependencies themselves, esp. when it comes to logging frameworks. Download commons-logging and add it to your classpath. Do as well with all other dependencies required by httpclient.


You need the dependencies. If you used maven, they would fetch themselves. Since you aren't, you've got to go download commons-logging (and perhaps others) and toss them in.


I'd like to update anyone coming here to the fact that all answers, and especially RocketSurgeon's, helped me solve my issue in the same sense. I had to download commons httpclient (legacy 3.1), logging and codec before my issues were resolved.

Also of note; using Eclipse I had to extract the .jar-files and import them into Buildpath using "add library". Did not work with only adding .zip-file.


If you dont want to use Maven, then

- build and launch, look at missing dependencies
- identify vendor, fetch the jar to resolve dependencies
- rinse/repeat until you have all dependencies satisfied
- done

anyway you will be never sure if all dependencies are 100% supplied. Its usually around 150% or 99%, but never in between

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜