Access restriction: The type BASE64Encoder is not accessible due to restriction

Problem

Downloaded a Java sample from Alexa API on Amazon service, imports it into Eclipse, but unable to compile and hits following “Access restriction” errors :


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

P.S Using JDK 1.6 and Eclipse IDE 4.2


import sun.misc.BASE64Encoder; // Access restriction error 

   //...
   result = new BASE64Encoder().encode(rawHmac);
     

Solution

Using sun.* package should be avoided, read this statement.

If you insist want to use sun.misc.BASE64Encoder, in Eclipse, right click on the project, properties -> Java compiler –> Errors/Warnings –> Deprecated and restricted API –> Forbidden reference (access rules), change the default “Error” to “Warning“. Now, your code should be able to compile, but with some warning messages.

eclipse-access-rules-error

The above solution is not recommended, you should replace sun.misc.BASE64Encoder with other BASE64 class like Apache common codec.


import org.apache.commons.codec.binary.Base64;

    //...	
    result = new Base64().encodeToString(rawHmac);

References

  1. Alexa Web Information Service
  2. Apache common codec
  3. Why Developers Should Not Write Programs That Call ‘sun’ Packages

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
machavez23
Guest
machavez23

Buenisimo me ayudo ! tome su Like !

Avinash
Guest
Avinash

Thanks Buddy, Tumne bachaa liyyaaa.

Nikhil
Guest
Nikhil

Thanks a lot, it worked for me…

Brandon
Guest
Brandon

Thanks for the tip. Who would have thought it was as easy as changing from “Error” to “Warning”. Are there any warning during runtime or only during compilation?

pracownia reklamy
Guest
pracownia reklamy

What’s up, all the time i used to check webpage posts here early in the daylight, as i like to gain knowledge of more and more.

365bet ton
Guest
365bet ton

It’s actually very complex in this active life to listen news on TV, so I simply use the web for that reason, and obtain the latest information.

dolnoslaska agencja reklamowa
Guest
dolnoslaska agencja reklamowa

Hello, I enjoy reading all of your post. I like to write a little comment to support
you.

Mudassir Shahzad
Guest
Mudassir Shahzad

You are the man! BASE64Encoder is a great tool :)
Thanks for sharing

Frisian
Guest
Frisian

Base64 encoding and decoding is part of the JDK since Java 6: http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/DatatypeConverter.html
No need to use the sun classes anymore.

Mudassir Shahzad
Guest
Mudassir Shahzad

Frisian, The only problem that I have with java jdk’s base64 encoding api is that it is licensed.