问题
从Amazon服务上的Alexa API下载了Java示例,将其导入Eclipse,但是无法编译,并因“ 访问限制 ”错误而出现:
Access restriction: The type BASE64Encoder is not accessible due to restriction
on required library
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar
使用JDK 1.6和Eclipse IDE 4.2的PS
import sun.misc.BASE64Encoder; // Access restriction error
//...
result = new BASE64Encoder().encode(rawHmac);
解
应避免使用sun.*
软件包,请阅读以下声明 。
如果您坚持要使用sun.misc.BASE64Encoder
,请在Eclipse中右键单击该项目,然后依次单击属性-> Java编译器->错误/警告->已弃用和受限制的API->禁止引用(访问规则),更改默认值“ 错误 “到” 警告 。 现在,您的代码应该可以编译了,但是带有一些警告消息。
不建议使用上述解决方案,您应该将sun.misc.BASE64Encoder
替换为其他BASE64类,例如Apache common codec 。
import org.apache.commons.codec.binary.Base64;
//...
result = new Base64().encodeToString(rawHmac);