is the sun.misc package still available in java?

21,058

Solution 1

I suggest you forget about the sun.misc.BASE64Encoder and use Apache Commons Base64 class. Here is the link: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html


Update (6/7/2017)

Using sun.misc.BASE64Encoder will cause a compilation error with Java 9 and it is already giving a warning in Java 8.

The right class to use is Base64 in java.util package.

Example:

import java.util.Base64;

Base64.getDecoder().decode(...);

Solution 2

There isn't a 'publicly available' (so to speak) class in Java for this. Consider using Apache Commons Codec package which contains an implementation of base64. Homepage is here: http://commons.apache.org/codec/.

You use sun.* or com.sun.* packages directly at your own risk. Backwards compatibility is not guaranteed for those.

Solution 3

See this question but IMHO the accepted answer is wrong. You never want to use the non-documented classes like anything starting with sun. The reason for this is that now your code depends on a particular JVM implementation (an IBM JVM might not have this for example).

The 2nd answer (with the most votes) is the one you want.

Solution 4

In general, Oracle recommends don't call "Sun" packages

http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

Moreover, in your case, there was a request for migrating the use of sun.misc.* to supported APIs (JDK-8066506), and it is related to sun.misc.BASE64Decoder:

sun.misc.BASE64Decoder, sun.misc.BASE64Encoder

Use java.util.Base64 @since 8 instead

source: http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

Solution 5

There is a big difference between a warning and an error so you need to read the message carefully to see what it what it says.

Classes in sun.misc can change and not be present on all JVMs. You are getter off using a library which has BASE64 encoding.

IMHO You can use sun.misc is you understand all the risks using the class has. However, in your case it is far better to avoid using any class under sun. and com.sun.

Share:
21,058
ict1991
Author by

ict1991

Updated on March 02, 2020

Comments

  • ict1991
    ict1991 over 4 years

    I would like to use the Base64 encoder of the package sun.misc.BASE64Encoder because I need to encode a password. However an error is being generated when I type in the import for this package.

    the message where the code for the encoder is to be used is the following:

    private synchronized static String hash(final String username, final String password) {
            DIGEST.reset();
            return new BASE64Encoder().encode(DIGEST.digest((username.toLowerCase() + password).getBytes()));
        }
    

    Is there an equivalent class in java which does the same thing? Or maybe, does someone know how to be able to get the code of the original class maybe please?

    thanks :)

  • ivantod
    ivantod almost 9 years
    If anybody is still reading this now, Java 8 finally includes a public Base64 encoder/decoder, so it's not necessary to use Apache Commons any more for this.