All Products
Search
Document Center

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

Last Updated:Mar 01, 2023

Problem description

Object Storage Service (OSS) SDK for Java returns NoClassDefFoundError or NoSuchFieldError, indicating that a package conflict may exist in the project. The following code shows the details of 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

The following code shows the details of 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)

Causes

NoClassDefFoundError occurs because the Java virtual machine cannot find a particular class at runtime which was available at compile time. For example, the Java virtual machine returns NoClassDefFoundError if a required method or a static member of a class is unavailable at runtime.

Solutions

The error indicates that a code issue exists. Check whether the JAR package in your code causes a conflict. You can use one of the following methods to resolve the JAR package conflict:

  • Use the same version of Apache HttpClient. Run mvn dependency:tree in the project directory to check the JAR package and the version that the project uses. For example, the following code shows that your project uses Apache HttpClient 4.3:

    • If OSS SDK for Java uses Apache HttpClient 4.4.1 and your project uses an Apache HttpClient that conflicts with Apache HttpClient 4.4.1, use Apache HttpClient 4.4.1 for your project and delete Apache HttpClient dependencies for other versions from the pom.xml file.

      Note

      Apache HttpClient versions provided in this topic are for illustration purposes only. Adjust your code based on the Apache HttpClient versions that you use.

    • If your project uses Commons HttpClient, conflicts may also occur. To resolve these conflicts, delete Commons HttpClient.

  • Resolve dependency conflicts. If your project is dependent on multiple third-party packages and the packages are dependent on different versions of Apache HttpClient, dependency conflicts may occur on your project. To resolve the conflicts, use dependency exclusions. For more information, see Maven Guides.

Applicable scope

  • OSS