42

I am developing a Java EE application in which I need Base64 Encoding/Decoding

So I added commons-codec-1.5.jar in WEB-INF/lib folder of my application and used

import org.apache.commons.codec.binary.Base64;

in the Java file.

During compile time, when I type Base64, it shows encodeBase64String method is available. But during runtime it is throwing an exception like this:

java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String

I have the JAR in the buildpath, but still I don't understand why it throws me the above error.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sreeram
  • 3,160
  • 6
  • 33
  • 44

8 Answers8

76

That method was introduced in Commons Codec 1.4. This exception indicates that you've an older version of Commons Codec somewhere else in the webapp's runtime classpath which got precedence in classloading. Check all paths covered by the webapp's runtime classpath. This includes among others the Webapp/WEB-INF/lib, YourAppServer/lib, JRE/lib and JRE/lib/ext. Finally remove or upgrade the offending older version.


Update: as per the comments, you can't seem to locate it. I can only suggest to outcomment the code using that newer method and then put the following line in place:

System.out.println(Base64.class.getProtectionDomain().getCodeSource().getLocation());

That should print the absolute path to the JAR file where it was been loaded from during runtime.


Update 2: this did seem to point to the right file. Sorry, I can't explain your problem anymore right now. All I can suggest is to use a different Base64 method like encodeBase64(byte[]) and then just construct a new String(bytes) yourself. Or you could drop that library and use a different Base64 encoder, for example this one.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the reponse BalusC.I searched my whole system for this jar.I could not find any old jar anywhere in my system.All i have is a commons-codec-1.5.jar in my Webapp/WEB-INF/lib – Sreeram Oct 07 '11 at 14:58
  • 2
    I updated my answer with a code line which should give you further insight as to where the `Base64` class is actually been loaded from during runtime. – BalusC Oct 07 '11 at 15:03
  • @BalusC-the above System.out.printed the following file:/C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/Project_name/WEB-INF/lib/commons-codec-1.5.jar – Sreeram Oct 07 '11 at 15:12
  • That is plain weird. I'd have guessed that you were using Glassfish 3.0.x which has version 1.3 in its lib, but I see that you're actually using Tomcat 7.0.x which shouldn't have any Commons Codec in its lib. Right now I can't suggest anything else than just using another method or to replace Commons Codec Base64 by a different Base64 encoder/decoder implementation. – BalusC Oct 07 '11 at 15:22
  • By the way, which JARs do you have more in your `/WEB-INF/lib`? Perhaps one of them is repackaging Commons Codec. Even though I'd have expected that the code source location would have returned it.. – BalusC Oct 07 '11 at 15:29
  • @BalusC-I was using Sun's encoding previously.I came through this [doc](http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html). Then i changed to apache commons-codec.I will try with the above and let you know. – Sreeram Oct 07 '11 at 15:31
  • 1
    Another strategy to pinpoint if its same class thats being picked or a different one is to use reflection to print out all public methods of Base64 class. – Usman Saleem Oct 07 '11 at 15:36
  • @BalusC- i have the following jars in the WEBINF/lib: activation.jar,aspectjweaver.jar,axis.jar,classes12.jar,commons-beanutils-1.8.3.jar,commons-codec-1.5.jar,commons-collections-3.2.jar,commons-dbcp-1.3.jar,commons-digester-1.8.jar,commons-discovery-0.4.jar,commons-logging-1.1.1.jar,commons-pool.jar,jaxrpc.jar,mail.jar,saaj.jar,servlet.jar,webserviceutils.jar,wsdl4j.jar – Sreeram Oct 07 '11 at 15:36
  • I don't recognize any which could have repackaged codec. But that `servlet.jar` definitely doesn't belong there. Remove it. Tomcat has already its own. – BalusC Oct 07 '11 at 15:43
  • 1
    @BalusC-I cleaned and build the project.Then i deployed my WAR into tomcat.Interestingly it started working fine.Please see this another question of mine regarding java [link](http://stackoverflow.com/questions/7687100/org-apache-http-util-encodingutils-getbytespostdata-base64-removing-from) – Sreeram Oct 08 '11 at 06:39
  • @BalusC, that line of code gives a null pointer error. anyway how can i access encoder and decoder methods in Base64.class, I am using eclipse and it can't locate those methods – Pankaj Nimgade Jan 07 '15 at 05:35
6

Some Google tooling such as GWT has an embedded version of commons-codec with a pre-1.4 Base64 class. You may need to make such tooling JARs inaccessible to your code by refactoring your project such that only the parts of your code that need that tooling can see the dependency.

Adam Augusta
  • 444
  • 4
  • 9
6

@Adam Augusta is right, One more thing

Apache-HTTP client jars also comes in same category as some google-apis.

org.apache.httpcomponents.httpclient_4.2.jar and commons-codec-1.4.jar both on classpath, This is very possible that you will get this problem.

This prove to all jars which are using early version of common-codec internally and at the same time someone using common-codec explicitly on classpath too.

Ram Patra
  • 16,266
  • 13
  • 66
  • 81
pkm1986
  • 195
  • 2
  • 6
2

Download this jar

It resolved my problem, this is 1.7.

asifaftab87
  • 1,315
  • 24
  • 28
1

I faced the same problem with JBoss 4.2.3 GA when deploying my web application. I solved the issue by copying my commons-codec 1.6 jar into C:\jboss-4.2.3.GA\server\default\lib

Ktonneh
  • 112
  • 8
  • This solved my problem. I am using JBoss 5.1.0 GA and deploy a EAR on this server. I need the commons-codec 1.6 for an api included in the project. The JBoss server has a commons-codec 1.3 jar in his lib folder. I want to deploy all libs needed by my project in the ear, than this error occurs. When I copy the commons-codec 1.6 in the lib folder (the old one still exist), it works. – jcomouth Sep 25 '15 at 14:01
1

You need the Apache Commons Codec library 1.4 or above in your classpath. This library contains Base64 implementation.

1

Try add 'commons-codec-1.8.jar' into your JRE folder!

0

Simply create an object of Base64 and use it to encode or decode, when using org.apache.commons.codec.binary.Base64 library

To Encode

Base64 ed=new Base64();

String encoded=new String(ed.encode("Hello".getBytes()));

Replace "Hello" with the text to be encoded in String Format.

To Decode

Base64 ed=new Base64();

String decoded=new String(ed.decode(encoded.getBytes()));

Here encoded is the String variable to be decoded

Community
  • 1
  • 1
Karan
  • 1
  • 5