A H M A D G O H A R

Please Wait For Loading

Ahmad Gohar Featured Image 1886_826

Fixing “Access Restriction” Errors for BASE64Encoder in Eclipse

August 27, 2013 Ahmad Gohar 0 Comments

Resolving the “Access Restriction” Error for BASE64Encoder in Eclipse

When using sun.misc.BASE64Encoder or sun.misc.BASE64Decoder in your project, Eclipse might show the following error:

Access restriction: The type BASE64Decoder is not accessible due to restriction on required library xxx.jar

This error occurs because sun.misc classes are part of non-public APIs that are discouraged for use in production. Eclipse flags this as a configuration issue by default.


Root Cause

  • sun.misc classes are internal, non-public APIs provided by the JDK. Using these classes is discouraged as they are not guaranteed to be available in future Java versions.
  • Eclipse enforces these restrictions by default and prevents you from using them.

Solution

Option 1: Change Eclipse Compiler Settings

  1. Navigate to Preferences:

    • Go to WindowPreferencesJavaCompilerErrors/Warnings.
  2. Adjust Settings:

    • Locate “Deprecated and restricted API”.
    • Change the setting to Warning or Ignore.
    • Locate “Forbidden and Discouraged References”.
    • Change the setting to Warning or Ignore.
  3. Apply Changes:

    • Click Apply and OK.
    • Clean and rebuild your project.

Option 2: Use an Alternative Base64 Encoder

Instead of using sun.misc.BASE64Encoder, switch to a more robust and modern library like Apache Commons Codec or Java 8’s built-in Base64 API.

Using Java 8’s Base64 API
import java.util.Base64;

public class Base64Example {
    public static void main(String[] args) {
        String originalInput = "Hello, World!";
        
        // Encoding
        String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
        System.out.println("Encoded: " + encodedString);

        // Decoding
        byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
        String decodedString = new String(decodedBytes);
        System.out.println("Decoded: " + decodedString);
    }
}
Using Apache Commons Codec
  1. Add Dependency:

    • Add Apache Commons Codec to your project.
    • For Maven:
      <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.15</version>
      </dependency>
      
  2. Code Example:

    import org.apache.commons.codec.binary.Base64;
    
    public class Base64Example {
        public static void main(String[] args) {
            String originalInput = "Hello, World!";
            
            // Encoding
            String encodedString = Base64.encodeBase64String(originalInput.getBytes());
            System.out.println("Encoded: " + encodedString);
    
            // Decoding
            byte[] decodedBytes = Base64.decodeBase64(encodedString);
            String decodedString = new String(decodedBytes);
            System.out.println("Decoded: " + decodedString);
        }
    }
    

Why Avoid sun.misc.BASE64Encoder?

  1. Not Standard:

    • sun.misc.BASE64Encoder is part of JDK internal APIs, and its usage is not recommended.
  2. Future Compatibility:

    • These classes might not be available in future versions of Java.
  3. Better Alternatives:

    • Java 8’s Base64 API and Apache Commons Codec are standard, widely used, and reliable.

Conclusion

While adjusting Eclipse settings can bypass the error, it’s better to use a modern, standard library for Base64 encoding and decoding. Java 8’s built-in Base64 API or Apache Commons Codec are excellent choices for robust and future-proof implementations.

Would you like more examples or assistance with integrating these libraries? 😊

author avatar
Ahmad Gohar
With over 18 years of experience in software architecture, Java technologies, and leadership, I specialize in crafting scalable, future-proof solutions for global organizations. Whether it’s transforming legacy systems, building cutting-edge cloud-native applications, or mentoring teams to excel, I’m committed to delivering value-driven results.

Leave A Comment