Access restriction: Is not accessible due to restriction on required library ..\jre\lib\rt.jar
Asked Answered
D

10

76

I am trying to modify some legacy code from while back and getting the following kind of errors:

Access restriction: The method create(JAXBRIContext, Object) from the type Headers is not accessible due to restriction on required library ..\jre\lib\rt.jar

for these import statements:

import com.sun.xml.internal.bind.api.JAXBRIContext;
import com.sun.xml.internal.ws.api.message.Header;
import com.sun.xml.internal.ws.api.message.Headers;
import com.sun.xml.internal.ws.developer.WSBindingProvider;

Been searching what this might mean and how to fix it, however not been able to find a clear answer. Some posts seem to suggest that I have some JARs included that implement classes that are now available as part of the core java distribution, but as far as I can see none of the JARs I include contain different/older versions of the above classes.

Anyone able to tell me what this error is all about and how I might go about fixing this?

Thanks for your help already in advance,

Olli

Drolet answered 13/2, 2012 at 19:24 Comment(1)
possible duplicate of Access restriction on class due to restriction on required library rt.jar?Fishworm
A
130

I ran into something similar, I think that the cause of the warning is Eclipse trying to discourage you from using the internal com.sun packages that are installed as part of your workspace JRE but which are not part of the public Java API.

As Justin says in his answer, changing your compiler settings can hide the warning. A more fine-grained approach is to modify your build path to explicitly allow access to the package in question:

  1. Open the Libraries tab of the Java Build Path project property window.
  2. Expand the JRE System Library entry.
  3. Select "Access rules" and hit the Edit button.
  4. Click the Add button in the resulting dialog.
  5. For the new access rule, set the resolution to Accessible and the pattern to "com/sun/xml/internal/**".

After adding this access rule, your project should build without these warning.

Ayah answered 17/5, 2012 at 19:19 Comment(8)
hmm, strange didn't work for me but that's in RAD 8 not Eclipse... the one below did work of course though, inexplicably (just add/remove the JRE)Screamer
+1 for adding specific access rule - rather than disabling all warnings. you rock!Teeter
I'm another satisfied customer. Thank you for this!Liv
Is it possible to save this setting?Swart
Is it "safe" to ignore the warning and access these internal com.sun.** classes? - if the given class is not part of the public API, can one be guaranteed that any JRE will contain the given class?Dunson
@Dunson That's the point of the warnings - these classes are subject to change at any point in time. If at all possible, you should avoid these internal classes in favor of using the public API. That said, many of them are part of OpenJDK and have been unchanged for years, so... it's a judgment call on your part.Ayah
Is it possible to somehow add an explicit dependency, fx in the pom file if using Maven, to address this risc? I would imagine not - an explicit dep. to rt.jar sounds strange. But it seems frustrating not being able to (securely) access classes that are known to exist in some jar.Dunson
Is there any official source saying that they are internal?Orchestral
P
79

Excellent answer already provide onsite here.

See the summary below:

  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.
Phare answered 10/7, 2012 at 22:54 Comment(3)
Worked for me after updating from an older jdk.Needlepoint
Thanks! Worked for me on Eclipse Kepler. Including the servlet-api-3.1.jar from the Jetty 9 distribution into my project caused the problem apparently. Or maybe it was the run jetty run plugin. Oh well, as long as it worked..Overreact
@AdityaSingh 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. Please refer accepted answer for this question.Roland
B
41

Not a true solution, but everywhere I looked the solution suggested was to simply tell Eclipse that those aren't errors. You can change it by going to Properties --> Java Compiler --> Errors Warnings --> Deprecated and restrited APIs --> Forbidden reference (acess rule), Change it from Error to Warning or Ignore.

Backset answered 6/3, 2012 at 15:14 Comment(2)
this also worked! simplest thing seems to be either to do this or add/remove as per the suggestion by @Phare .Screamer
Worked for me as well but I have to wonder why the warning is there in the first place.Eldredge
K
15

i've solved this issue with these steps: expand your project, right click "JRE System Library" > Properties > choose 3rd option "Workspace default JRE" > OK . Hope it help you too

Kweichow answered 16/1, 2015 at 10:55 Comment(1)
By the way, this will get reset everytime you do Maven > Update in case you're using Maven and you didn't set: <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> for exampleFoss
C
8

In Eclipse:
Project -> properties -> java Build Path -> libraries

Remove existing JRE System Library, then Add Library -> JRE System library -> next -> ok

Error will be removed.

Crosscheck answered 24/4, 2016 at 21:53 Comment(0)
S
5

I had the same problem when my plugin was depending on another project, which exported some packages in its manifest file. Instead of changing access rules, I have managed to solve the problem by adding the required packages into its Export-Package section. This makes the packages legally visible. Eclipse actually provides this fix on the "Access restriction" error marker.

Sn answered 18/3, 2014 at 9:48 Comment(0)
D
1

In the eclipse environment where you execute your java programs, take the following steps:

  1. Click on Project just above the menu bar in eclipse.
  2. Click on properties.
  3. Select libraries, click on the existing library and click Remove on the right of the window.
  4. Repeat the process and now click add library, then select JRE system library and click OK.
Darkling answered 30/11, 2016 at 18:28 Comment(0)
B
0

I'm responding to this question because I had a different way of fixing this problem than the other answers had. I had this problem when I refactored the name of the plugins that I was exporting. Eventually I had to make sure to fix/change the following.

  1. The product file's dependencies,
  2. The plugin.xml dependencies (and make sure it is not implicitly imported using the imported packages dialog).
  3. The run configuration plug-ins tab. Run As..->Run Configurations->Plug-ins tab. Uncheck the old plugins and then click Add Required Plug-ins.

This worked for me, but your mileage may vary.

Bountiful answered 21/4, 2014 at 15:57 Comment(0)
A
0

I just changed project facet to 1.7 and it worked.

Avraham answered 21/10, 2015 at 12:59 Comment(0)
L
-2

Go to Buildpath

Remove Existing JRE and add new JRE library which contain Jdk1.6 and finish Now clean all project and build again

I think this way you can resolved your error

Lexicology answered 15/1, 2014 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.