使用阿里云OSS Java SDK时org/apache/http/ssl/TrustStrategy

在使用阿里云对象存储服务(Object Storage Service,简称 OSS)提供的Java SDK的时候,遇到的问题

Posted by Steven on 2018-11-30

一、前言

在使用阿里云对象存储服务(Object Storage Service,简称 OSS)提供的Java SDK的时候,有时候会遇到这样的报错:

1
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy

具体报错信息如下:

1
2
3
4
5
6
7
8
9
10
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
at com.aliyun.oss.demo.HelloOSS.main(HelloOSS.java:77)
Caused by: java.lang.ClassNotFoundException: org.apache.http.ssl.TrustStrategy at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 3 more

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.j
ava:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.j
ava:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactor
y.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConne
ctionFactory.java:82)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConne
ctionFactory.java:95)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConne
ctionFactory.java:104)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConn
ectionFactory.java:62)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:572)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnecti
onManager.java:174)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnecti
onManager.java:158)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnecti
onManager.java:149)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnecti
onManager.java:125)
at com.aliyun.oss.common.comm.DefaultServiceClient.createHttpClientConnectionManager(DefaultS
erviceClient.java:237)
at com.aliyun.oss.common.comm.DefaultServiceClient.<init>(DefaultServiceClient.java:78) at com.aliyun.oss.OSSClient.<init>(OSSClient.java:268)
at com.aliyun.oss.OSSClient.<init>(OSSClient.java:193)
at OSSManagerImpl.upload(OSSManagerImpl.java:42)
at OSSManagerImpl.main(OSSManagerImpl.java:63)

二、错误原因

错误原因是OSS Java SDK使用了Apache httpclient 4.4.1,而我们的工程使用了与非4.4.1版本的Apache httpclient或commons-httpclient jar包,即Jar包冲突。

这里说一下,怎么查看我们的工程用的是哪个jar包以及版本,在工程目录下查看自己的pom搜索httpclient ,看看用的是什么版本

三、问题解决

包冲突有两种解决方案:

  • 使用统一版本。
    如果您的工程使用与Apache httpclient 4.4.1冲突的版本,请您使用4.4.1版本,并在pom.xml删除其它版本的Apache httpclient依赖。如 果您的工程使用了commons-httpclient,也可能存在冲突,请删除commons-httpclient。
  • 解除依赖冲突。
    如果您的工程依赖多个第三方包,而第三方包又依赖不同版本的Apache httpclien t,您的工程里会有依赖冲突,请使用exclusion解除,具体可以参考:
    https://maven.apache.org /guides/introduction/introduction-to-optional-and-excludes-dependencies.html?spm=a2c4g.1 1186623.2.17.58cb5413AK8Ym0