-->
Skip to main content

Solution : java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.(I)V]

Solution : java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V]

This java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V] happens when we compile our source class using common codec jar version 1.4 or more but at runtime, it founds common-codec jar either equal to 1.3 or lesser than that in which this constructor was not present.

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V]

Exception Trace will look like this:
java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V
    at org.apache.http.impl.auth.BasicScheme.authenticate(BasicScheme.java:166) [httpclient-4.5.10.jar:4.5.10]
    at org.apache.http.impl.auth.HttpAuthenticator.doAuth(HttpAuthenticator.java:233) [httpclient-4.5.10.jar:4.5.10]
    at org.apache.http.impl.auth.HttpAuthenticator.generateAuthResponse(HttpAuthenticator.java:213) [httpclient-4.5.10.jar:4.5.10]
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:262) [httpclient-4.5.10.jar:4.5.10]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) [httpclient-4.5.10.jar:4.5.10]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) [httpclient-4.5.10.jar:4.5.10]
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) [httpclient-4.5.10.jar:4.5.10]
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) [httpclient-4.5.10.jar:4.5.10]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) [httpclient-4.5.10.jar:4.5.10]

Root Cause

1. The signature org.apache.commons.codec.binary.Base64.(I)V is referring to a constructor that takes an int argument. According to GrepCode, all versions of Apache Commons-Codec since version 1.4 have this constructor. But version 1.3 does not. 

2. This exception indicates that you have an older version of Commons Codec somewhere else in the web app's runtime classpath which got precedence in classloading.

3. This is caused when a different version of the Base64 class is used at compile time to the one picked up at runtime.

Other Use Case

You have multiple jars in your classpath which inbuild have common-codec-1.3.jar or less. So we need to exclude common-codec-1.3 jar dependency from those jars so that your latest jar takes priority.

Finding the culprit jar

To find the culprit jar which is causing this java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64 error we will use Apache Maven Dependency Plugin, which will help in identifying all the jars which have a common codec jar inside them or usage of common codec jar in the project.

In your maven project run this command below to find the dependency tree 

 mvn dependency:tree -Dincludes=commons-codec:commons-codec

Now find the tree which shows common-codec version less than equal to 1.3 which will be shown below which in my case found in another jar I am using in my project

 [INFO] \- org.apache.myfaces.core:myfaces-impl:jar:1.2.12:compile
 [INFO]    \- commons-codec:commons-codec:jar:1.3:compile

In your case, it may be in your pom directly but in most cases, some other jars have an inbuilt jar which we should exclude.

Common Codec Dependency Tree

To Exclude it from myfaces-impl jar we simply edit our pom where this dependency is included like below
<dependency>
  <groupId>org.apache.myfaces.core</groupId>
  <artifactId>myfaces-impl</artifactId>
  <version>1.2.12</version>
  <exclusions>
    <exclusion>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
    </exclusion>
  </exclusions>
</dependency>
Now when we run the above command mvn dependency:tree -Dincludes=commons-codec:commons-codec again we will not have that jar in our myfaces-impl-jar and the problem will be resolved.

Now if we don't find a common codec jar while running our application which will cause us 
java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
then we have to add a new common-codec dependency with either the latest version or the version which is compatible with the jar from which the class got compiled earlier.

For example, at compile time if it got compiled from common-codec-1.11-jar then at runtime we can include the dependency in pom with version 1.11 like below.
<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.11</version>
</dependency>
Which will resolve java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64 error.

Conclusion

In this tutorial, we have covered how to solve java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.<init>(I)V] exception. 

Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

Happy Learning!!!

Comments