Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 11 : Patch sun.misc.BASE64Encoder

This question is not a duplicate of java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder.

I am trying to update the Java Version (to 11) in my app and one of the libraries in my app uses sun.misc.BASE64Encoder class, so I inevitably get this exception:

Caused by: java.lang.ClassNotFoundException: sun.misc.BASE64Encoder
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

Now, the way to solve would be to use java.util.Base64 or apache commons as the other answers suggest. However, the problem is, this library belongs to a third party and I don't have the source code for it. There doesn't seem to be a new version that is not using these classes. So, to work around this issue, I did the following:

  • Created package sun.misc
  • Copied both BASE64Encoder and CharacterEncoder classes in this package

But now, I get a compilation error saying The package sun.misc conflicts with a package accessible from another module: jdk.unsupported.

Is there any way to get around this error? If not, can I add a jar to the dependencies that contains these classes? I am just trying to make sure these classes are available to that third party library at runtime (either via my own source code or via a jar that bundles these classes in).

like image 497
Darshan Mehta Avatar asked Jan 23 '26 20:01

Darshan Mehta


1 Answers

The JDK 11 replacement for sun.misc.BASE64Encoder and sun.miscBASE64Decoder is java.util.Base64.Encoder and java.util.Base64.Decoder.

Typical code in JDK 1.8 would look like:

String encoded = new BASE64Encoder().encode(bBytes);
byte[] decoded = new BASE64Decoder().decodeBuffer(encoded);

The JDK 11 replacements which would generate identical values are:

import java.util.Base64;
// ...
String encoded = Base64.getEncoder().encodeToString(bBytes);
byte[] decoded = Base64.getDecoder().decode(encoded);
like image 189
Kinjal Dixit Avatar answered Jan 26 '26 09:01

Kinjal Dixit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!