How to get the JAR file for sun.misc.BASE64Encoder class?

33,190

Solution 1

Don't use sun.* classes. For Base64 in Android, use its native class or add a library to your project that does this (Apache commons, etc.). Alternatively just copy the Android source code to your project (in your own package), if you need to use it on pre-2.2 devices.

Solution 2

Since Java 8 you can use java.util.Base64 class:

byte[] original = "Base64".getBytes(StandardCharsets.UTF_8);

// encode
byte[] toBase64 = Base64.getEncoder().encode(original);

// decode
byte[] fromBase64 = Base64.getDecoder().decode(toBase64);

// output
System.out.println(new String(toBase64)); //QmFzZTY0
System.out.println(new String(fromBase64)); //Base64
System.out.println(Arrays.equals(original, fromBase64)); //true

See also: How can I convert a string into a GZIP Base64 string?

Solution 3

For me it is resolved with simple steps, I was using Java11 JDK.As, in this version Base64 classes are deprecated, we are getting this issue. Changing to Java 8 solved the issue. Steps: In Eclipse,

  1. Go to, Windows-> Preferences-> Installed JREs
  2. Add-> select JDK8 or lesser version of JDK->Add
  3. Make it as default JDK by selecting this JDK
  4. Apply and Close

And, Done.

Thank you.

Solution 4

  1. Just add rt.jar to the build path of the project from path C:\Program Files\AdoptOpenJDK\jdk-8.0.202.08\jre\lib
  2. Copy the jar and paste in the libraries folder of the project.
Share:
33,190
Santhosh
Author by

Santhosh

I'm what I'm

Updated on September 21, 2021

Comments

  • Santhosh
    Santhosh over 2 years

    I have some code in my Activity class that uses sun.misc.BASE64Encoder class. But it is showing java.lang.NoClassDefFoundError : sun.misc.BASE64Encoder. Do I need the jar? Where can I download it from?

    • KV Prajapati
      KV Prajapati over 12 years
      Don't use non-public API. Read this thread for more details. stackoverflow.com/questions/5549464/…
    • Santhosh
      Santhosh over 12 years
      I'am new to android. Could you please explain me where i'am wrong? How can i encrypt my string using that class?
    • KV Prajapati
      KV Prajapati over 12 years
      Encryption is not encoding. To do the encoding, you can use the BASE64.The base64 maps 6-bit blocks of binary data into 64 different character representations.
    • Santhosh
      Santhosh over 12 years
      okay. but i have to encrypt my data. How can i encrypt ?
  • Stephen C
    Stephen C over 2 years
    This doesn't solve the problem. It puts off solving the problem. The real solution is to fix your code so that it doesn't use the internal class ... that it should never have been using in the first place.
  • Stephen C
    Stephen C over 2 years
    This doesn't solve the problem. It puts off solving the problem. The real solution is to fix your code so that it doesn't use the internal class ... that it should never have been using in the first place.
  • Stephen C
    Stephen C over 2 years
    Yea ... but Java 1.5 is about 20 years out of date. In fact, this particular class was dropped in Java 9, and (AFAIK) has never been present on the Android platform, which is what this question was about.
  • WMS
    WMS about 2 years
    I am using java-8-241 and rt.jar contains Base64Encoder and Base64Decoder, but the openjdk only have Base64, and Base64 was find in both jres. So, is better using Base64 with java-8.
  • WMS
    WMS about 2 years
    I am using java-8-241 and rt.jar contains Base64Encoder and Base64Decoder, but the openjdk only have Base64, and Base64 was find in both jres. So, is better using Base64 with java-8