Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder

Tags:

java

http

I would like to connect some domain with java code. I can connect the domain in the browser as follow: http://username:[email protected]

I tryed the following:

    String enc = "username" + ":" + "password";
            String encoded = new sun.misc.BASE64Encoder().encode(loginPassword.getBytes());
URL url = new URL("domain.com");
            URLConnection conn = url.openConnection();
            conn.setRequestProperty ("Authorization", "Basic " + encoded);

I get the error: 401/Unutorized... java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder

Is there any solution I can try?.

like image 371
Max_Salah Avatar asked Apr 17 '15 06:04

Max_Salah


1 Answers

Oracle has announced the removal of those classes in Java 9.

You should not be using classes that are in sun.* packages - those classes are not part of the public API of Java and can change in any new Java version.

See Why Developers Should Not Write Programs That Call 'sun' Packages in Oracle's documentation.

Instead of using class sun.misc.BASE64Encoder:

If you are using Java 8, then use class java.util.Base64 for Base 64 encoding and decoding.

Otherwise, use a third-party library, for example class org.apache.commons.codec.binary.Base64 from Apache Commons Codec.

like image 188
Jesper Avatar answered Nov 15 '22 16:11

Jesper