java.lang.NoClassDefFoundError:sun/misc/BASE64Encoder [英] java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder

查看:1667
本文介绍了java.lang.NoClassDefFoundError:sun/misc/BASE64Encoder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java代码连接某些域.我可以按照以下方式在浏览器中连接域: http://username:password@domain.com

I would like to connect some domain with java code. I can connect the domain in the browser as follow: http://username:password@domain.com

我尝试了以下操作:

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

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

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

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

Is there any solution I can try?.

推荐答案

Oracle具有宣布在Java 9中删除了这些类.

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

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

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"的程序包在Oracle文档中.

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

代替使用类sun.misc.BASE64Encoder:

如果您使用的是Java 8,请使用类 java.util.Base64 用于Base 64编码和解码.

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

否则,请使用第三方库,例如类 Apache Commons的binary/Base64.html"rel =" noreferrer> org.apache.commons.codec.binary.Base64 编解码器.

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

这篇关于java.lang.NoClassDefFoundError:sun/misc/BASE64Encoder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆