HttpClient NoClassDefFoundError 错误
我正在尝试从 HttpClient 4.0.1 运行示例应用程序。它是示例部分中的 ClientMultiThreadedExecution.java 文件。我将这些文件放入类路径中:apache-mime4j-0.6.jar
;commons-codec-1.3.jar
;commons-logging-1.1.1。 jar
;httpclient-4.0.1.jar
;httpcore-4.0.1.jar
;httpmime-4.0.1.jar
并且文件编译正确。在运行时我收到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
我是否缺少引用?这似乎是一个类路径错误,但我无法弄清楚要包含哪个 jar 文件?预先感谢您的帮助。
I am trying to run a sample application from HttpClient 4.0.1. It is the file ClientMultiThreadedExecution.java from the examples section. I put in these files in the classpath: apache-mime4j-0.6.jar
;commons-codec-1.3.jar
;commons-logging-1.1.1.jar
;httpclient-4.0.1.jar
;httpcore-4.0.1.jar
;httpmime-4.0.1.jar
and the file compiles correctly. At runtime I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Am I missing a reference? It seems like a classpath error but I can't figure out which jar file to include? Thank you in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此异常表明运行时类路径中缺少提到的类。
有多种方法可以指定运行时类路径,具体取决于您执行程序的方式。由于一个像样的 IDE 可以透明地从您手中获取这一切,我敢打赌您是在命令提示符下运行它。
如果您通过
java.exe -jar
将其作为 JAR 文件运行或双击该文件,则需要在该文件的Class-Path
条目中指定类路径JAR 的 MANIFEST.MF 文件。请注意,每当您执行 JAR 时,%CLASSPATH%
环境变量以及-cp
和-classpath
参数都会被忽略 。如果您通过
java.exe
将其作为“普通”Java 应用程序运行,那么您需要在-cp
或-classpath< /代码> 参数。请注意,每当您使用此参数时,
%CLASSPATH%
环境变量都会被忽略。无论哪种方式,类路径都应该包含以(半)冒号分隔的 JAR 文件路径字符串(绝对路径或相对于当前工作目录的路径)。例如
(如果您使用的是 Unix/Linux,请使用冒号而不是分号作为路径分隔符)
This exception tells that the mentioned class is missing in the runtime classpath.
There are several ways to specify the runtime classpath, depending on how you're executing the program. Since a decent IDE takes this all transparently from your hands, I bet that you're running it in a command prompt.
If you're running it as a JAR file by
java.exe -jar
or doubleclicking the file, then you need to specify the classpath in theClass-Path
entry of the JAR'sMANIFEST.MF
file. Note that the%CLASSPATH%
environment variable and-cp
and-classpath
arguments are ignored whenever you execute a JAR.If you're running it as a "plain vanilla" Java application by
java.exe
, then you need to specify it in the-cp
or-classpath
argument. Note that whenever you use this argument, the%CLASSPATH%
environment variable is ignored.Either way, the classpath should exist of a (semi)colonseparated string of paths to JAR files (either absolute paths or relative to current working directory). E.g.
(if you're on Unix/Linux, use colon instead of semicolon as path separator)
该类位于 httpclient-4.0.1.jar 中(我刚刚下载了它以确保),所以我怀疑您没有将其正确放入类路径中。
你是如何编译和运行你的代码的?
That class is in httpclient-4.0.1.jar (I've just downloaded it to be sure) so I suspect you haven't put it in the classpath properly.
How are you compiling and running your code?
在 cloudera-quickstart-vm-5.8.0 中运行 Eclipse Luna 2 (4.4.2) 我必须添加以下
..然后它就没有错误了。
Running Eclipse Luna 2 (4.4.2) inside cloudera-quickstart-vm-5.8.0 I had to add the following
... and then it worked without errors
当我遇到这个问题时,发现当我将 Fluent API 添加为 Maven 依赖项时,它导入了与我已经使用的版本不同的 HTTPClient API 版本。两个版本的 API 都打包在生成的 JAR 的 lib 文件夹中。版本冲突是导致此错误的原因。
将条目添加到类路径中将解决该问题,因为您只需手动指定要使用的版本。但是,为了解决根本问题,我只需要在重建之前删除目标文件夹(或运行 maven clean)。这会删除所有“缓存的”库 JAR,并且在下一个版本中,仅重新下载正确的 JAR。
希望对某人有帮助!
When I experienced this issue, it turned out that when I added the Fluent API as a Maven dependency, it imported a different version of the HTTPClient API than the one I was already using. Both versions of the API were packaged in the resulting JAR's lib folder. The version conflict is what caused this error.
Adding entries to your classpath will fix the problem, because you're just manually specifying what version to use. However, to fix the underlying problem, I just needed to delete my target folder before rebuilding (or run maven clean). This removed any "cached" library JARs, and on the next build, only re-downloaded the correct one.
Hope that helps somebody!
当您在连接、发送等方面遇到问题并且您想知道方法时,我只想对异常进行小改进。
httpclient-4.5.13.jar
httpcore-4.5.13.jar
+++ 表示异常 +++
javax.activation-api-1.2.0.jar
javax.annotation-api-1.3.2.jar
javax.xml.soap-api-1.4.0.jar
jaxb-api-2.3.1.jar
I want to small improve only for Exception when you will problem with connection, sending, ... AND you want to know WAY.
httpclient-4.5.13.jar
httpcore-4.5.13.jar
+++ for Exception +++
javax.activation-api-1.2.0.jar
javax.annotation-api-1.3.2.jar
javax.xml.soap-api-1.4.0.jar
jaxb-api-2.3.1.jar