开发者

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

开发者 https://www.devze.com 2023-03-28 17:33 出处:网络
I have some code in my Activity class that uses sun.misc.BASE64Encoder class. But it is showing java.lang.NoClassDefFoundError : sun.misc.BASE64Encode开发者_Go百科r. Do I need the jar? Where can I dow

I have some code in my Activity class that uses sun.misc.BASE64Encoder class. But it is showing java.lang.NoClassDefFoundError : sun.misc.BASE64Encode开发者_Go百科r. Do I need the jar? Where can I download it from?


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.


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?


  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.


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.


It's available in rt.jar, that comes with Sun java runtimes.

In 1.5.0_06 : jre\lib\rt.jar

0

精彩评论

暂无评论...
验证码 换一张
取 消