Java HttpClient NoClassDefFoundError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3231215/
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-13 17:37:28  来源:igfitidea点击:

HttpClient NoClassDefFoundError

javaclasspathhttpclientclassnotfound

提问by user389753

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.jarand the file compiles correctly. At runtime I get the following error:

我正在尝试从 HttpClient 4.0.1 运行示例应用程序。它是示例部分中的 ClientMultiThreadedExecution.java 文件。我把在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并且文件编译正确。在运行时,我收到以下错误:

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

我缺少参考吗?这似乎是一个类路径错误,但我不知道要包含哪个 jar 文件?预先感谢您的帮助。

采纳答案by BalusC

This exception tells that the mentioned class is missing in the runtimeclasspath.

此异常表明运行时类路径中缺少提到的类。

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.

有多种方法可以指定运行时类路径,具体取决于您执行程序的方式。由于一个体面的 IDE 可以从您的手中透明地处理这一切,我敢打赌您是在命令提示符下运行它的。

If you're running it as a JAR file by java.exe -jaror doubleclicking the file, then you need to specify the classpath in the Class-Pathentry of the JAR's MANIFEST.MFfile. Note that the %CLASSPATH%environment variable and -cpand -classpatharguments are ignoredwhenever you execute a JAR.

如果您通过java.exe -jar或双击该文件将其作为 JAR 文件运行,那么您需要Class-Path在 JARMANIFEST.MF文件的条目中指定类路径。需要注意的是%CLASSPATH%环境变量,-cp并且-classpath参数被忽略,只要你执行一个JAR。

If you're running it as a "plain vanilla" Java application by java.exe, then you need to specify it in the -cpor -classpathargument. Note that whenever you use this argument, the %CLASSPATH%environment variable is ignored.

如果您通过 将它作为“普通”Java 应用程序运行java.exe,那么您需要在-cpor-classpath参数中指定它。请注意,无论何时使用此参数,%CLASSPATH%都会忽略环境变量。

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.

无论哪种方式,类路径都应该存在一个(半)冒号分隔的 JAR 文件路径字符串(绝对路径或相对于当前工作目录)。例如

java -cp .;/path/to/file1.jar;/path/to/file2.jar com.example.MyClass

(if you're on Unix/Linux, use colon instead of semicolon as path separator)

(如果您使用的是 Unix/Linux,请使用冒号而不是分号作为路径分隔符)

回答by Jon Skeet

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.

该类在 httpclient-4.0.1.jar 中(我刚刚下载了它以确保)所以我怀疑您没有正确地将它放在类路径中。

How are you compiling and running your code?

你如何编译和运行你的代码?

回答by Jacob Ensor

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.

当我遇到这个问题时,结果证明当我将 Fluent API 添加为 Maven 依赖项时,它导入的 HTTPClient API 版本与我已经使用的版本不同。两个版本的 API 都打包在生成的 JAR 的 lib 文件夹中。版本冲突是导致此错误的原因。

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.

将条目添加到您的类路径将解决该问题,因为您只是手动指定要使用的版本。但是,为了解决潜在的问题,我只需要在重建(或运行 maven clean)之前删除我的目标文件夹。这删除了所有“缓存”的库 JAR,在下一次构建中,只重新下载了正确的。

Hope that helps somebody!

希望对某人有所帮助!

回答by George Fisher

Running Eclipse Luna 2 (4.4.2) inside cloudera-quickstart-vm-5.8.0 I had to add the following

在 cloudera-quickstart-vm-5.8.0 中运行 Eclipse Luna 2 (4.4.2) 我必须添加以下内容

  • apache-httpcomponents-httpcore.jar
  • httpclient-4.5.3.jar
  • httpclient-cache-4.5.3.jar
  • apache-httpcomponents-httpcore.jar
  • httpclient-4.5.3.jar
  • httpclient-cache-4.5.3.jar

... and then it worked without errors

...然后它没有错误地工作