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

Johan
  • 74,508
  • 24
  • 191
  • 319
user567602
  • 909
  • 1
  • 9
  • 15
  • possible duplicate of [Access restriction on class due to restriction on required library rt.jar?](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar) – Gavin S. Yancey Nov 15 '14 at 23:36

10 Answers10

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.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
Alex
  • 13,811
  • 1
  • 37
  • 50
  • 1
    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) – bcmoney Jun 19 '13 at 15:22
  • 2
    +1 for adding specific access rule - rather than disabling all warnings. you rock! – rjha94 Jul 16 '13 at 14:55
  • I'm another satisfied customer. Thank you for this! – TonyK Jul 17 '14 at 18:26
  • Is it possible to save this setting? – Raffi Khatchadourian Jun 26 '15 at 14:44
  • 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? – Hervian May 19 '16 at 11:31
  • 1
    @Hervian 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. – Alex May 19 '16 at 13:07
  • 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. – Hervian May 19 '16 at 15:06
  • Is there any official source saying that they are internal? – Franklin Yu Mar 07 '19 at 20:03
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.
Community
  • 1
  • 1
Abimbola
  • 854
  • 6
  • 5
  • Worked for me after updating from an older jdk. – Casey Murray Jan 13 '14 at 00:25
  • 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.. – faizal May 12 '14 at 16:19
  • 1
    @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](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar) question. – Ashish Kumar Dec 30 '16 at 08:32
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.

Justin
  • 419
  • 3
  • 2
  • this also worked! simplest thing seems to be either to do this or add/remove as per the suggestion by @Abimbola . – bcmoney Jun 19 '13 at 15:23
  • Worked for me as well but I have to wonder why the warning is there in the first place. – mister270 Jul 10 '14 at 16:59
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

Candra
  • 151
  • 1
  • 2
  • By the way, this will get reset everytime you do Maven > Update in case you're using Maven and you didn't set: ` 1.8 1.8 ` for example – ACV Sep 22 '15 at 08:07
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.

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
Bilal Ashraf
  • 77
  • 1
  • 3
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.

Val
  • 1
  • 8
  • 40
  • 64
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.
rfornal
  • 5,072
  • 5
  • 30
  • 42
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.

Lucas
  • 2,514
  • 4
  • 27
  • 37
0

I just changed project facet to 1.7 and it worked.

Magno C
  • 1,922
  • 4
  • 28
  • 53
-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