[英]java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder
I would like to connect some domain with java code. 我想用Java代码连接某些域。 I can connect the domain in the browser as follow: http://username:password@domain.com
我可以按以下方式在浏览器中连接域: http:// username:password@domain.com
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?. 有什么办法可以尝试吗?
Oracle has announced the removal of 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' Packages in Oracle's documentation. 请参阅Oracle文档中的开发人员为何不应该编写调用“ sun”程序包的程序 。
Instead of using class sun.misc.BASE64Encoder
: 而不是使用类
sun.misc.BASE64Encoder
:
If you are using Java 8, then use class java.util.Base64
for 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.Base64
from Apache Commons Codec . 否则,请使用第三方库,例如Apache Commons Codec的类
org.apache.commons.codec.binary.Base64
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.