java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29692146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:44:09  来源:igfitidea点击:

java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder

javahttp

提问by Max_Salah

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

我想用java代码连接一些域。我可以在浏览器中连接域,如下所示: 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

我收到错误:401/Unutorized... java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder

Is there any solution I can try?.

有什么我可以尝试的解决方案吗?

回答by Jesper

Oracle has announced the removalof those classes in Java 9.

Oracle 已宣布在 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.

您不应该使用sun.*包中的类 - 这些类不是 Java 公共 API 的一部分,可以在任何新的 Java 版本中更改。

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

请参阅Oracle 文档中的为什么开发人员不应编写调用“sun”包的程序

Instead of using class sun.misc.BASE64Encoder:

而不是使用 class sun.misc.BASE64Encoder

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

如果您使用的是 Java 8,则使用类java.util.Base64进行 Base 64 编码和解码。

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

否则,请使用第三方库,例如org.apache.commons.codec.binary.Base64来自Apache Commons Codec 的类。