Access restriction on class due to restriction on required library rt.jar?

663,459

Solution 1

There's another solution that also works.

  1. Go to the Build Path settings in the project properties.
  2. Remove the JRE System Library
  3. Add it back; Select "Add Library" and select the JRE System Library. The default worked for me.

This works because you have multiple classes in different jar files. Removing and re-adding the JRE lib will make the right classes be first. If you want a fundamental solution make sure you exclude the jar files with the same classes.

For me I have: javax.xml.soap.SOAPPart in three different jars: axis-saaj-1.4.jar, saaj-api-1.3.jar and the rt.jar

Solution 2

http://www.digizol.com/2008/09/eclipse-access-restriction-on-library.html worked best for me.

On Windows: Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rules): -> change to warning

On Mac OS X/Linux: Eclipse -> Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rules): -> change to warning

Solution 3

I met the same problem. I found the answer on the website: http://www.17ext.com.
First, delete the JRE System Libraries. Then, import JRE System Libraries again.

Solution 4

My guess is that you are trying to replace a standard class which ships with Java 5 with one in a library you have.

This is not allowed under the terms of the license agreement, however AFAIK it wasn't enforced until Java 5.

I have seen this with QName before and I "fixed" it by removing the class from the jar I had.

EDIT http://www.manpagez.com/man/1/java/ notes for the option "-Xbootclasspath:"

"Applications that use this option for the purpose of overriding a class in rt.jar should not be deployed as doing so would contravene the Java 2 Runtime Environment binary code license."

The http://www.idt.mdh.se/rc/sumo/aJile/Uppackat/jre/LICENSE

"Java Technology Restrictions. You may not modify the Java Platform Interface ("JPI", identified as classes contained within the "java" package or any subpackages of the "java" package), by creating additional classes within the JPI or otherwise causing the addition to or modification of the classes in the JPI. In the event that you create an additional class and associated API(s) which (i) extends the functionality of the Java platform, and (ii) is exposed to third party software developers for the purpose of developing additional software which invokes such additional API, you must promptly publish broadly an accurate specification for such API for free use by all developers. You may not create, or authorize your licensees to create, additional classes, interfaces, or subpackages that are in any way identified as "java", "javax", "sun" or similar convention as specified by Sun in any naming convention designation."

Solution 5

I have been getting this error too, but my project is built on the command line using Maven and the tycho compiler (it's a set of OSGi plugins). After masses of sifting through people having the same problem but fixing it in Eclipse rather than on the command line, I found a message on the Tycho developer forum that answered my question, using configuration in pom.xml to ignore the compiler warning about the access restriction:

<plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>tycho-compiler-plugin</artifactId>
    <version>${tycho.version}</version>
    <configuration>
        <compilerArgument>-warn:+discouraged,forbidden</compilerArgument>
    </configuration>
</plugin>

More information can be found in the Tycho FAQ. This took me AGES to work out, so I figured I would assist anyone else trying to fix these access restriction errors from the command line by posting this answer.

Share:
663,459
sal
Author by

sal

cat herder, technologist, software developer

Updated on August 19, 2021

Comments

  • sal
    sal over 2 years

    I'm attempting to compile Java 1.4 code that was created by IBM's WSDL2Java on Java5 without recreating the stubs and saw this error in Eclipse.
    I'm under the assumption that the stubs generated should just compile as long as the runtime jars are available (they are).

    Access restriction: The type QName is not accessible due to restriction on required library C:\Program Files\Java\jdk1.5.0_16\jre\lib\rt.jar

    The full class name is javax.xml.namespace.QName

    What exactly is going on here? Is this a case where I am trying to refactor a pig from sausage? Am I better off recreating the stubs?

    • Tim Büthe
      Tim Büthe about 15 years
      I don't get it, why you not just compile it somewhere else and run it in you targeted (so I guess) 1.4 environment?
    • sal
      sal about 15 years
      The eventual target environment is jboss4.2 on jdk5.
    • OscarRyz
      OscarRyz almost 14 years
      About the "protected" status: In StackOverflow Nothing says "Thanks" or "me too" as an upvote ;)
    • will
      will over 11 years
      See the big Most-Voted-Answer ... Ignore 96% of the rest of this page. Search: "Nels Beckman", Feb 1 '10 at 4:09
    • ADTC
      ADTC almost 10 years
      What worked for me was to edit/change the JRE System Library from Execution environment (or Workspace default) to Alternate JRE (I selected the same Java version). You must also ensure (1) correct order in Order and Export tab, (2) correct compliance level in Java Compiler settings (same as the selected Java version).
  • sal
    sal about 15 years
    that's it. one of the jars in the path had the QName class in it. find . -name "*.jar" -print -exec unzip -t {} \; |grep "QName" found it.
  • Adrian Mouat
    Adrian Mouat over 13 years
    Could you provide a reference about not being allowed to replace classes which ship with Java? All I found in the licence agreement was restrictions related to distributing Java itself, not Java programs, but I didn't look for very long.
  • Adrian Mouat
    Adrian Mouat over 13 years
    This might work, but it is not a proper solution. You need to understand why the access restriction existed in the first place. It will also hide all future cases of this, which may be more important!
  • Nels Beckman
    Nels Beckman over 13 years
    Huh, apparently you answered this question the same way as I did, several months before. Not sure why I didn't see your answer back then...
  • docwhat
    docwhat about 13 years
    Is it an Eclipse bug or are we accidentally working-around the restriction (and violating the license terms)? If it's an Eclipse bug, then is there a bug filed?
  • Nels Beckman
    Nels Beckman about 13 years
    @Doctor I never used this for any code that was particularly important, so I didn't investigate any further... If you find anything out please let us know.
  • stolsvik
    stolsvik over 12 years
    @AdrianMouat that is pretty much irrelevant. If I want it to go away, I want it to go away. But surely - one shalln't code against non-public APIs, no.
  • Adrian Mouat
    Adrian Mouat over 12 years
    @stolsvik - you've lost me; are you saying the reason why the problem exists is irrelevant?
  • URL87
    URL87 almost 12 years
    where is "Add Library" in eclipse ?
  • Alexei Blue
    Alexei Blue almost 12 years
    @URL87 If you right click on the project folder go to "Build Path...", "Configure Build Path", "Libraries" (tab), you should see "Add Library" as one of the buttons on the right... This worked a charm for me as well, excellent answer
  • will
    will over 11 years
    I have this problem on ONE method. I imagine the use of an alternative JDK (like OpenJDK is a better option). That said for one thing it 'might be cool' once. NOT in production code. Not for an on going design effort. I can't tell you how many man-days get lost on this kind of cr-hack.
  • david blaine
    david blaine almost 11 years
    @AdrianMouat - makes sense. I'd hate to do something like this in a nuclear reactor - Too much heat in control room ? So, disable all warnings. Make big headlines next day. :P
  • Steven
    Steven almost 11 years
    Fair enough, but you've basically replicated the wording of the accepted answer by Nels Beckman.
  • Admin
    Admin over 10 years
    actually, I was having this "error" message when I set the JRE to one of those default "execution environments" in Eclipse. Even if I bind the execution environment to the exact match JDK, eclipse still insists to say it's an error, so disabling the warning was actually a pretty good solution for me.
  • Greg Haskins
    Greg Haskins over 10 years
    Same issue here. In my case, using Maven which defaults to 1.5 if unspecified.
  • user1676075
    user1676075 over 10 years
    The better solution under recent versions of Eclipse is not to delete the JRE System Library, but to go to "Order and Export" tab and just move JRE System Library to the bottom (which is effectively what deleting and adding it does, but you don't need to delete and add to do it).
  • ggb667
    ggb667 over 10 years
    You might need to un-check the allow projects to override and also change the workspace java compliance settings.
  • Stef
    Stef over 9 years
    This solved it for me. The other answer didn't. That's because I work on a project where we use lots of deprecated code (very old codebase) and unfortunately it's not gonna change too soon. So this solution is the only one that works.
  • MattC
    MattC about 9 years
    And for me it was not the same JRE System Library that I added back. In my case it was originally 5, and I when I went to add back in, the default pointed to 7. Thanks!
  • Dominic Cerisano
    Dominic Cerisano over 8 years
    Switched from openJDK to sunJDK for the proprietary codecs, then I got this "restrictions" error and thought I was overflowed. Thanks.
  • muyong
    muyong over 7 years
    This also work for me. My project was built with Java 7 but now I changed to 8, so the version change may be the reason. Delete the JRE System Libraries them add it back doesn't help.
  • ttulinsky
    ttulinsky almost 7 years
    I put the JRE lib at the bottom of the order list, then deleted and re-added it. When at the bottom of the list I got errors: The method getTextContent() is undefined for the type Element. Then I put it back at the top of the list and it finally worked. Eclipse Neon, JDK 1.8. – ttulinsky 16 hours ago
  • Philip Rego
    Philip Rego about 6 years
    Remember to put this in your POM so it doesn't change back when updated. <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
  • Aravamudhan
    Aravamudhan about 6 years
    This is 2018 and eclipse version is 5.0. This bug/problem still exists. Thanks a lot @NelsBeckman. Your answer has helped me after 3/4 of decade since it was posted.
  • Marvo
    Marvo almost 6 years
    Mine was already set to 1.7...79, so I was panicking. But I simply selected it again, clicked Apply, and the error went away. Whew.
  • hyphan
    hyphan almost 5 years
    Wow. This helped here too - changing from an "execution environment" to an "alternate JRE". If anyone has any logical explanation for this.. (here it occurred after changing the project-faucet Java from 1.5 (5.0 in the .settings config file) to 1.8. Changing from error to warning in global prefs (see other answer) did not help: still errors. Its about old sun classes we use from package com.sun.image.codec.*)
  • Phillip
    Phillip almost 5 years
    Right clicking on the project and choosing 'Refresh Gradle project' always seems to undo any of these changes
  • tom_mai78101
    tom_mai78101 over 2 years
    2021, encountered a missing javax.swing.Icon class. There is another step I need to do, which is to restart the IDE after removing the JRE system library, and do it before adding back in the required JRE system library (Java 16 for me).