All Products
Search
Document Center

Object Storage Service:How do I handle NoClassDefFoundError or NoSuchFieldError in OSS SDK for Java?

Last Updated:Mar 20, 2026

Both NoClassDefFoundError and NoSuchFieldError point to a JAR dependency conflict — your build resolved one version of Apache HttpClient at compile time, but the JVM loaded a different version at runtime. Run mvn dependency:tree to identify the conflict, then apply one of the fixes below.

Identify the conflict

Run the following command in your project directory and look for multiple versions of httpclient or httpcore:

mvn dependency:tree

A conflict appears as two entries with the same artifact name but different versions:

[INFO] +- com.aliyun.oss:aliyun-sdk-oss:jar:3.x.x:compile
[INFO] |  \- org.apache.httpcomponents:httpclient:jar:4.4.1:compile
[INFO] +- com.example:your-library:jar:1.0.0:compile
[INFO]    \- org.apache.httpcomponents:httpclient:jar:4.3.6:compile

In this example, OSS SDK for Java requires Apache HttpClient 4.4.1, but another library pulls in 4.3.6. Maven selects one version — if it selects the older one, the classes OSS SDK for Java needs at runtime are missing or incompatible, which causes NoClassDefFoundError or NoSuchFieldError.

The following are typical stack traces for each error type.

NoClassDefFoundError:

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

NoSuchFieldError:

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
 at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
 at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
 at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
 at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:82)
 at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:95)
 at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:104)
 at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:62)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:572)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:174)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:158)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:125)
 at com.aliyun.oss.common.comm.DefaultServiceClient.createHttpClientConnectionManager(DefaultServiceClient.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)
Note: The Apache HttpClient version 4.4.1 shown above is for illustration only. Check your actual mvn dependency:tree output and adjust accordingly.

Fix the conflict

Option 1: Pin Apache HttpClient to the version OSS SDK for Java requires (recommended)

Add an explicit <dependencyManagement> entry to your pom.xml to pin Apache HttpClient to 4.4.1 (or the version OSS SDK for Java declares):

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.4.1</version>
    </dependency>
  </dependencies>
</dependencyManagement>

This forces Maven to use 4.4.1 across all transitive dependencies, regardless of what individual libraries request.

If your project also depends on Commons HttpClient (the older commons-httpclient artifact), remove it — it conflicts with Apache HttpClient and is no longer maintained.

Option 2: Exclude the conflicting version from specific dependencies

If you cannot change the required version globally, exclude the transitive Apache HttpClient dependency from the library that pulls in the conflicting version:

<dependency>
  <groupId>com.example</groupId>
  <artifactId>your-library</artifactId>
  <version>1.0.0</version>
  <exclusions>
    <exclusion>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
    </exclusion>
  </exclusions>
</dependency>

For more details on Maven dependency exclusions, see Introduction to Optional and Excludes Dependencies.