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