is the sun.misc package still available in java? [duplicate]
Asked Answered
O

5

7

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 :)

Oldtime answered 26/12, 2011 at 21:43 Comment(1)
Short answer: "yes, but don't use them"Excitation
B
11

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(...);
Bonspiel answered 26/12, 2011 at 21:58 Comment(0)
C
5

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.

Chufa answered 26/12, 2011 at 21:56 Comment(1)
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.Chufa
B
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.

Butterscotch answered 26/12, 2011 at 21:46 Comment(0)
N
3

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

Newscast answered 1/3, 2018 at 15:32 Comment(0)
S
1

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.

Somniferous answered 26/12, 2011 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.