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?
- Just add rt.jar to the build path of the project from path
C:\Program Files\AdoptOpenJDK\jdk-8.0.202.08\jre\lib
- 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,
- Go to, Windows-> Preferences-> Installed JREs
- Add-> select JDK8 or lesser version of JDK->Add
- Make it as default JDK by selecting this JDK
- 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
精彩评论