线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / http / ConnectionReuseStrategy: [英] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ConnectionReuseStrategy:

查看:223
本文介绍了线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / http / ConnectionReuseStrategy:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Eclipse在maven项目中作为Java Application运行,我收到以下运行时错误。错误如下所示。

I am trying to run as Java Application in a maven project with Eclipse and I am getting the following run time error.The error is shown below.


线程main中的异常java.lang.NoClassDefFoundError:
org / apache / http / ConnectionReuseStrategy at
com.wang.testMaven.App.main(App.java:16)引起by:
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)

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ConnectionReuseStrategy at com.wang.testMaven.App.main(App.java:16) Caused by: 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!" );

    }
}

我的pom.xml如下所示

My pom.xml is shown below

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

任何想法..谢谢

推荐答案

我意识到有相同的groupId和artifactId存在几个依赖关系。

I realised that there are couple of dependencies with the same groupId and 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的依赖项。

Actually you should have only one dependency with the same groupId and artifactId.

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

So firstly I removed httpcore:4.1-alpha1 dependency and executed the test class. I got the exception as

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,我得到了同样的例外。

Then I used httpcore:4.1-alpha1 instead of httpcore-4.1, I got the same exception.

因此当我根据 SO链接,我得到了例外 -

And hence when I updated the httpcore version to 4.3.3 based on SO link, I got the exception as -

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并且它也工作正常。)。 此处提供了可用的版本。请通过此处查找工作代码。

Finally updating the httpcore version to 4.4, it worked perfectly fine. So it should work for all httpcore version jars above 4.4. (I tested for 4.4.4 and it also worked fine.). Version available are mentioned here. Please find the working code over here.

这篇关于线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / http / ConnectionReuseStrategy:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆