Java Apache HttpClient 抛出 NoClassDefFoundError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1776415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:30:50  来源:igfitidea点击:

Apache HttpClient throws a NoClassDefFoundError

javahttpclient

提问by rauch

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

我有: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.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: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***
    
  1. 我刚刚从http://hc.apache.org/downloads.cgi下载了“Commons HttpClient 3.1 (legacy)” 。
  2. 我使用 IDE NetBeans 创建了新应用程序。
  3. 我专注于 Projects -> Libraries -> Add JAR/Folder 并在这里添加了 commons-httpclient-3.1.jar
  4. 现在,我可以写“import org.apache.commons.httpclient.*;” 还行吧。
  5. 但是,如果我尝试添加任何包含以下内容的语句:

    public static void main(String[] args) {
        HttpClient client = new HttpClient();
    }
    
  6. 它编译得很好。

  7. 但是当我运行这个项目时,我收到以下错误:

    ***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.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: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.

我尝试下载“HttpClient 4.0 (GA)”。但结果是一样的。

采纳答案by mhaller

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.

您所遇到的只是缺少依赖项。Java 开发人员习惯于自己收集他们的依赖项,尤其是。谈到日志框架。下载 commons-logging 并将其添加到您的类路径中。对 httpclient 所需的所有其他依赖项也做同样的事情。

回答by bmargulies

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.

您需要依赖项。如果您使用 maven,它们会自行获取。既然你不是,你必须去下载 commons-logging(也许还有其他)并将它们扔进去。

回答by mhaller

If you dont want to use Maven, then

如果你不想使用Maven,那么

- 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

无论如何,您永远无法确定是否 100% 提供了所有依赖项。它通常在 150% 或 99% 左右,但从未介于两者之间

回答by oalmgren

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.

我想告诉任何来到这里的人,所有答案,尤其是 RocketSurgeon 的答案,都以同样的方式帮助我解决了我的问题。在我的问题得到解决之前,我必须下载 commons httpclient(旧版 3.1)、日志记录和编解码器。

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.

同样值得注意的是;使用 Eclipse 我必须提取 .jar 文件并使用“添加库”将它们导入 Buildpath。仅添加 .zip 文件不起作用。