访问限制: 由于对所需库. . jre lib rt.jar 的限制而无法访问

我试图修改一些遗留代码,从而得到以下类型的错误:

访问限制: 由于对所需库. . jre lib rt.jar 的限制,无法访问 Header 类型的方法 create (JAXBRIContext,Object)

对于这些进口报表:

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;

我一直在寻找这可能意味着什么以及如何解决这个问题,但是没有找到一个明确的答案。一些文章似乎暗示我包含了一些 JAR,它们实现了现在作为核心 Java 发行版的一部分可用的类,但就我所见,我包含的 JAR 中没有一个包含上述类的不同/旧版本。

有谁能告诉我这个错误是怎么回事以及我该如何修复这个错误吗?

谢谢你提前帮忙,

奥利

162800 次浏览

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.

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.

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.

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

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.

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.

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

I just changed project facet to 1.7 and it worked.

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.

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.