我意识到有几个依赖项具有相同的 groupId 和 artifactId。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.1-alpha1</version>
</dependency>
And
<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,我得到了同样的异常。
因此,当我根据以下内容将 httpcore 版本更新为 4.3.3 时SO link,我得到的例外是 -
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.