【问题标题】:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ConnectionReuseStrategy:线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/http/ConnectionReuseStrategy:
【发布时间】:2016-04-20 13:53:30
【问题描述】:

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

线程“主”java.lang.NoClassDefFoundError 中的异常: org/apache/http/ConnectionReuseStrategy 在 com.wang.testMaven.App.main(App.java:16) 原因: java.lang.ClassNotFoundException: org.apache.http.ConnectionReuseStrategy 在 java.net.URLClassLoader.findClass(URLClassLoader.java:381) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在 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!" );

    }
}

我的 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>

任何想法..谢谢

【问题讨论】:

  • 尝试清理并重建项目
  • @Abdelhak 是的,我试过很多次了,但是没有任何效果!
  • 因为你在Eclipse上运行,你应该检查httpcore是否在eclipse项目的classpath中?还有一点是检查httpcore-4.1alpha1是否有class:ConnectionReuseStrategy

标签: java maven-2


【解决方案1】:

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

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

还有

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

实际上你应该只有一个具有相同 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)

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

因此,当我根据SO link 将 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)

最后将 httpcore 版本更新到 4.4,它工作得非常好。所以它应该适用于 4.4 以上的所有 httpcore 版本 jar。 (我测试了 4.4.4,它也工作得很好。)。可用版本提到here。请通过here 找到工作代码。

【讨论】:

  • @asg谢谢你的回答!但是当我使用你的工作代码时它不起作用。也许我认为这是我的 Eclipse 的原因。
  • 我使用的是 STS 3.7.0.20,它在 STS 上运行良好。
  • @asg我知道原因。我已将 .jar 文件复制到我的 JDK 文件夹中!我删除它们后它就可以工作了。!
  • 很高兴知道。谢谢!
  • 感谢@asg 的回答!我根据 Spring 依赖文档将我的 org.apache.httpcomponents httpcore 版本更改(升级)为 4.4.13,它对我有用。参考这里:docs.spring.io/spring-boot/docs/current/reference/html/…
猜你喜欢
  • 1970-01-01
  • 2011-02-10
  • 2017-03-24
  • 1970-01-01
  • 2013-09-29
  • 2013-03-19
  • 2018-08-09
  • 2017-11-22
  • 2021-07-22
相关资源
最近更新 更多