线程“main”中的异常 java.lang.NoClassDefFoundError: org/apache/http/ConnectionReuseStrategy:

Ton*_*i.W 4 java maven-2

我试图在 Eclipse 的 maven 项目中作为 Java 应用程序运行,但出现以下运行时错误。错误如下所示。

线程“main”中的异常 java.lang.NoClassDefFoundError: org/apache/http/ConnectionReuseStrategy at com.wang.testMaven.App.main(App.java:16) 由:java.lang.ClassNotFoundException: org.apache.http .ConnectionReuseStrategy at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

我的代码如下所示

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        HttpClient httpClient = HttpClientBuilder.create().build();
        System.out.println( "Hello World!" );

    }
}
Run Code Online (Sandbox Code Playgroud)

我的 pom.xml 如下所示

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.wang</groupId>
  <artifactId>testMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testMaven</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.1-alpha1</version>
</dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient-cache</artifactId>
            <version>4.5.2</version>
        </dependency>
  </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

任何想法..谢谢

asg*_*asg 7

我意识到有几个依赖项具有相同的 groupId 和 artifactId。

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.1-alpha1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

实际上,您应该只有一个具有相同 groupId 和 artifactId 的依赖项。

所以首先我删除了 httpcore:4.1-alpha1 依赖项并执行了测试类。我得到了例外

java.lang.NoClassDefFoundError: org/apache/http/config/Lookup
    at com.test.so.Test1.test(Test1.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Run Code Online (Sandbox Code Playgroud)

然后我使用 httpcore:4.1-alpha1 而不是 httpcore-4.1,我得到了同样的异常。

因此,当我根据SO 链接将 httpcore 版本更新为 4.3.3 时,我得到了异常 -

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ssl/SSLContexts
    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:966)
    at com.amitg.so.App.main(App.java:11)
Run Code Online (Sandbox Code Playgroud)

最后将 httpcore 版本更新到 4.4,它工作得很好。所以它应该适用于 4.4 以上的所有 httpcore 版本的 jar。(我测试了 4.4.4,它也运行良好。)。这里提到可用的版本。请在这里找到工作代码。