1

I am getting the following exception in my web application:

 java.util.concurrent.ExecutionException: java.lang.NoSuchMethodError:     org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString([B)Ljava/lang/String;

The commons-codec-1.5.jar is added in my classpath. I am building it using Ant and have added the dependencies manually. Through other discussions on the same issue I found out that adding the source of the this library can solve the issue but that didn't work for me. I also read that having another library with the same class can cause the conflict and possibly that class may not have this method which results in the error. However, I have double checked that there is no other version of the same library. Is it possible that some other library has the same class in it? If yes, then how can I identify that and resolve the problem?

adarsh hegde
  • 1,353
  • 2
  • 21
  • 43

1 Answers1

3

As you allude to in your question, this is caused when a different version of the Base64 class is used at compile time to the one picked up at runtime. Since you are using Ant (rather than Maven for example), it should be easier to find the culprit since you don't have to worry about transitive dependencies.

The first thing to do is use your IDE to open the class Base64 (ctrl+N in IntelliJ), this will highlight how many different version of this library are on your classpath. In my case there were 2. If you have more than one then you've found your culprit.

If you only have one version on your classpath, then the only other place this class can reside is in the lib directory of Tomcat. You may have to open the jar files manually to see if any contain the same package org.apache.commons.codec.binary....

StuPointerException
  • 7,117
  • 5
  • 29
  • 54
  • Thank you very much @StuPointerException I was able to find the same package inside csrfguard jar. But the csrfguard jar contains a newer version- 1.9 of the commons-codec jar and has the method encodeBase64URLSafeString() for which I am getting the NoSuchMethodError. My jar is of version 1.5 which also has the method. Is this really causing the problem? – adarsh hegde Jan 19 '17 at 10:24
  • Strange, looking at the API the method was added in 1.4 and the signature hasn't changed since so I suspect not. – StuPointerException Jan 19 '17 at 12:14
  • I couldn't find the root cause for this issue. However I solved the problem by adding the source code for commons-codec in my project. Thus, when the war is packaged the class files for Base64 will be in WEB-INF\classes. The class loader will scan this location before WEB-INF\lib and thus will avoid the conflict due to the jars. – adarsh hegde Jan 23 '17 at 14:59