How to Fix Access restriction: The type BASE64Decoder is not accessible due to restriction Error in Eclipse? [Solution]

Hello guys, if you have been using Eclipse for Java development then you might have seen this dreaded  "Access restriction: The type BASE64Decoder is not accessible due to restriction Error" before. This error comes when you are trying to encode String into Base64 using BASE64Decoder in Eclipse because the class BASE64Decoder is not part of JDK's public API, it comes from sun.misc package which is non-public. Though this class is present in JDK and JRE and allows you to encode and decode String into base 64, any access to this class from Eclipse flags as an error in Eclipse IDE. If you compile and run the same program from the command line or Netbeans you will not get this error.

Now there are two ways to solve this error, The first and simple one is to change the Eclipse settings to consider using BASE64Decoder as a warning instead of Error. This will solve the problem immediately and will allow you to run the program right from Eclipse.

The second and better solution is to use a public API for your base64 encoding and decoding purpose, just like the one introduced in Java 8. This means you can switch to JDK8 or JRE 8 in your Eclipse setting and this error will go away. Alternatively, you can use Apache Commons package or other open-source JAR for encoding String using base 64 algorithm

Cause of Access restriction: The type BASE64Decoder is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar

This error comes because class BASE64Decoder is part of the sun.misc package which is not part of JDK API. If you want to learn more about Java 8 base 64 encoding and other enhancements I suggest you join a comprehensive Java course like given in this list of Java courses. It contains the most up-to-date course and updated to cover new features from recent Java releases.

Solution: Access restriction: The type BASE64Decoder is not accessible due to restriction

Change the Eclipse Settings to consider this as a warning than an error, for that you can follow the below steps :
  • Go to Window, Preferences, Java, Compiler, Error/Warnings.
  • Select Deprecated and Restricted API. Change it to a warning
  • Change forbidden and Discouraged Reference and change it to a warning. (or as your need.)

BTW, this is the general problem and will occur if you try to access any other class from the sun.misc package.

Alternatively, switch to Java 8 to use a new and shiny base64 encoder and decoder or if you are not yet ready for Java 8, then look for an open-source API which provides public API for base 64 encoding and decode.

Here is how you can switch to Java 8 in your Eclipse Project, just change the compile setting to point to JRE 8 as shown below screenshot:

Eclipse - Access restriction: The type BASE64Decoder is not accessible due to restriction Error [Solved[

Though changing settings in Eclipse can help you to avoid this error, your project should never depend upon Sun's internal classes. Using internal, non-public classes may break compatibility with future Java releases because the implementation and location of these classes may change without information.

Java is very good in protecting backward compatibility but I don't think it will care for non-public API like the sun.misc.

In short, you should stop using this class for base 64 encoding and decoding and switch to Java 8. If JDK 8 is not an option then consider using general-purpose libraries like Apache Commons Code or Google's Guava.

Related Java and Eclipse tutorials you may like to explore
If you like this tutorial and wants to learn more productivity tips, tricks, and new features of Eclipse IDE, then you can also see the following guides and tutorials:
  • The Java Developer RoadMap (guide)
  • How to attach the source code of a JAR file in Eclipse? (steps)
  • How to debug a Java Program in Eclipse IDE? (guide)
  • How to do the static import in Java Eclipse? (solution)
  • How to solve Eclipse No Java Virtual Machine found the error? (solution)
  • How to fix the JAR dependency not working in Eclipse Maven Plugin? (answer)
  • How to fix Eclipse Not able to connect Internet or Market Place error? (solution)
  • How do you create JUnit tests in Eclipse IDE? (guide)
  • Eclipse shortcut for System.out.println() statements (shortcut)
  • How to escape String literal in a Java file when posted on Eclipse? (solution)
  • How to compare two files in Eclipse IDE? (guide)
  • How to increase the console buffer size in Eclipse? (solution)
  • How to make an executable JAR file in Eclipse? (steps)
  • How to fix an Unsupported major.minor version 51.0 error in Eclipse? (solution)
  • How to fix must override superclass method error in Eclipse? (solution)
  • How to decompile class files in Eclipse? (solution)
  • How to set java.library.path in Eclipse IDE? (steps)
  • Step by Step Guide to Remote Debug a Java Application in Linux (guide)

Thanks for reading this article so far. If you like these Eclipse keyboard shortcuts, then please share this article with your friends and colleagues. If you have any questions or feedback, please drop a note.

P.S. - If you are looking for some free online courses to learn Eclipse better and improve your productivity then I suggest you check out this list of 6 Free Courses to Learn Eclipse and JUnit which not only teach you Eclipse better but also introduce you to JUnit, one of the most popular Java testing library.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.