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?
Asked
Active
Viewed 4.1k times
6

Santhosh
- 4,956
- 12
- 62
- 90
-
2Don't use non-public API. Read this thread for more details. http://stackoverflow.com/questions/5549464/import-sun-misc-base64encoder-got-error-in-eclipse/5549512#5549512 – KV Prajapati Aug 18 '11 at 05:05
-
I'am new to android. Could you please explain me where i'am wrong? How can i encrypt my string using that class? – Santhosh Aug 18 '11 at 05:50
-
2Encryption 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. – KV Prajapati Aug 18 '11 at 06:05
-
okay. but i have to encrypt my data. How can i encrypt ? – Santhosh Aug 18 '11 at 06:11
5 Answers
5
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.

Nikolay Elenkov
- 52,576
- 10
- 84
- 84
-
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 Apr 20 '22 at 18:09
4
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?
-
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 Apr 20 '22 at 18:07
2
- 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.

Avinash Dalvi
- 8,551
- 7
- 27
- 53

Gagan Chaudhary
- 21
- 1
-
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 Sep 21 '21 at 07:16
1
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.

Amulya Koppula
- 150
- 1
- 5
-
1This 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 Sep 21 '21 at 07:14
0
It's available in rt.jar, that comes with Sun java runtimes.
In 1.5.0_06 : jre\lib\rt.jar

user1893074
- 103
- 1
- 3
-
1Yea ... 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. – Stephen C Sep 21 '21 at 07:24