Access restriction: The constructor NotSupposedToUseThisClass() is not accessible due to restriction on required library

So following from my previous post about compiling Java 5 with a Java 6 compiler and the real reason that I am posting today, was to talk about a curly error message I got in Eclipse today.

I had the same Eclipse 3.7 project both on Windows and Mac, but only the Mac version complained with a very unusal message.

sun.misc.Base64 on mac JDK 5

The complaint was against an internal sun.misc.Base64Encoder class which every 2nd Java dev on the internet recommends replacing its internal implementation with an Apache Commons alternative.

Initially I thought it had something to do with the manifest and access restrictions preventing code outside of the jar accessing the internal code – something I recall the JavaPosse talking about from their days of Java Studio. Since it was only isolated to the Mac JDK, perhaps Apple had included these restrictions in the manifest for the classes.jar that these classes were defined.

Turns out it has to do with Eclipse.  I found out that when I add a JRE/JDK in Eclipse, it appears to add Access Rules to a bunch of select classes in the classes jar.  Its a white list.  Anything not there is forbidden.

I don’t know what defines this, and they are ‘non modifiable’ but adding in an access rule at the top leads this to go.

Java Build Path with Access Rules avoided

Looks like this has been tormenting people since Eclipse 3.3

Leave a Reply