Java 由于对所需库 rt.jar 的限制而导致对类的访问限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/860187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access restriction on class due to restriction on required library rt.jar?
提问by sal
I'm attempting to compile Java 1.4code that was created by IBM'sWSDL2Javaon Java5without 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).
我正在尝试编译由IBM 的WSDL2Java在Java5上创建的Java 1.4代码,而无需重新创建存根,并在Eclipse 中看到此错误。
我假设生成的存根只要运行时可用(它们可用)就应该编译。jars
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
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
完整的类名是 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?
这里到底发生了什么?这是我试图从香肠重构猪的情况吗?我最好重新创建存根吗?
采纳答案by Nels Beckman
There's another solution that also works.
还有另一种解决方案也有效。
- Go to the Build Pathsettings in the project properties.
- Remove the JRE System Library
- Add it back; Select "Add Library"and select the JRE System Library. The default worked for me.
- 转到项目属性中的构建路径设置。
- 删除JRE 系统库
- 加回来;选择“添加库”并选择JRE 系统库。默认对我有用。
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.
这是有效的,因为您在不同的 jar 文件中有多个类。删除并重新添加 JRE 库将使正确的类成为第一位。如果您想要一个基本的解决方案,请确保排除具有相同类的 jar 文件。
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
对我来说,我有:javax.xml.soap.SOAPPart
在三个不同的罐子里:axis-saaj-1.4.jar
,saaj-api-1.3.jar
和rt.jar
回答by Peter Lawrey
My guess is that you are trying to replace a standard class which ships with Java 5 with one in a library you have.
我的猜测是,您正试图用您拥有的库中的一个类替换 Java 5 附带的标准类。
This is not allowed under the terms of the license agreement, however AFAIK it wasn't enforced until Java 5.
根据许可协议的条款,这是不允许的,但是 AFAIK 直到 Java 5 才强制执行。
I have seen this with QName before and I "fixed" it by removing the class from the jar I had.
我之前在 QName 中看到过这个,我通过从我拥有的 jar 中删除类来“修复”它。
EDIT http://www.manpagez.com/man/1/java/notes for the option "-Xbootclasspath:"
编辑 http://www.manpagez.com/man/1/java/选项“-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."
“不应部署使用此选项来覆盖 rt.jar 中的类的应用程序,因为这样做会违反 Java 2 运行时环境二进制代码许可证。”
The http://www.idt.mdh.se/rc/sumo/aJile/Uppackat/jre/LICENSE
该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."
“Java 技术限制。您不得通过在 JPI 中创建附加类或以其他方式导致添加或修改 JPI 中的类。如果您创建了一个额外的类和相关的 API,它 (i) 扩展了 Java 平台的功能,并且 (ii) 向第三方软件开发人员公开为了开发调用此类附加 API 的附加软件,您必须及时广泛发布此类 API 的准确规范,供所有开发人员免费使用。您不得创建或授权您的被许可人创建附加类、接口、或以任何方式标识为“java”、“javax”、“sun”或 Sun 在任何命名约定指定中指定的类似约定的子包。
回答by sanwanxian
I met the same problem. I found the answer in the website:http://www.17ext.com.
First,delete the JRE System Libraries. Then,import JRE System Libraries again.
我遇到了同样的问题。我在网站上找到了答案:http: //www.17ext.com。
首先,删除 JRE 系统库。然后,再次导入 JRE 系统库。
I don't know why.However it fixed my problem,hope it can help you.
我不知道为什么。但是它解决了我的问题,希望它可以帮助你。
回答by scommab
http://www.digizol.com/2008/09/eclipse-access-restriction-on-library.htmlworked best for me.
http://www.digizol.com/2008/09/eclipse-access-restriction-on-library.html对我来说效果最好。
On Windows:Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rules): -> change to warning
在 Windows 上:Windows -> 首选项 -> Java -> 编译器 -> 错误/警告 -> 弃用和受限 API -> 禁止引用(访问规则): -> 更改为警告
On Mac OS X/Linux:Eclipse -> Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rules): -> change to warning
在 Mac OS X/Linux 上:Eclipse -> 首选项 -> Java -> 编译器 -> 错误/警告 -> 弃用和受限 API -> 禁止引用(访问规则): -> 更改为警告
回答by Mayur
- Go to the Build Path settings in the project properties.
Windows -> Preferences -> Java Compiler
- Remove the JRE System Library
- Add another JRE with a "perfect match"
- clean and build your project again. It worked for me.
- 转到项目属性中的构建路径设置。
Windows -> Preferences -> Java Compiler
- 删除 JRE 系统库
- 添加另一个具有“完美匹配”的 JRE
- 清理并再次构建您的项目。它对我有用。
回答by Dorus
I just had this problem too. Apparently I had set the JRE to 1.5 instead of 1.6 in my build path.
我也刚遇到这个问题。显然,我在构建路径中将 JRE 设置为 1.5 而不是 1.6。
回答by Mr. Napik
In the case you are sure that you should be able to access given class, than this can mean you added several jars to your project containing classes with identical names (or paths) but different content and they are overshadowing each other (typically an old custom build jar contains built-in older version of a 3rd party library).
如果您确定您应该能够访问给定的类,那么这可能意味着您向项目添加了几个 jar,其中包含具有相同名称(或路径)但内容不同的类,并且它们相互遮蔽(通常是旧习惯) build jar 包含内置的第 3 方库的旧版本)。
For example when you add a jar implementing:
例如,当您添加一个 jar 实现时:
a.b.c.d1
a.b.c.d2
but also an older version implementing only:
但也是仅实现的旧版本:
a.b.c.d1
(d2 is missing altogether or has restricted access)
Everything works fine in the code editor but fails during the compilation if the "old" library overshadows the new one - d2 suddenly turns out "missing or inaccessible" even when it is there.
在代码编辑器中一切正常,但如果“旧”库掩盖了新库,则在编译过程中会失败 - d2 突然变得“丢失或无法访问”,即使它在那里。
The solution is a to check the order of compile-time libraries and make sure that the one with correct implementation goes first.
解决方案是检查编译时库的顺序,并确保具有正确实现的库排在第一位。
回答by Arun B Chandrasekaran
Sorry for updating an old POST. I got the reported problem and I solved it as said below.
很抱歉更新旧的 POST。我收到了报告的问题,并按照下面的说明解决了它。
Assuming you are using Eclipse + m2e maven plugin, if you get this access restriction error, right click on the project/module in which you have the error --> Properties --> Build Path --> Library --> Replace JDK/JRE to the one that is used in eclipse workspace.
假设您使用的是 Eclipse + m2e maven 插件,如果您收到此访问限制错误,请右键单击出现错误的项目/模块 --> 属性 --> 构建路径 --> 库 --> 替换 JDK/ JRE 到 eclipse 工作区中使用的那个。
I followed the above steps and the issue is resolved.
我按照上述步骤操作,问题已解决。
回答by Dawngerpony
I have been getting this error too, but my project is built on the command line using Maven and the tychocompiler (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 forumthat answered my question, using configuration in pom.xml
to ignore the compiler warning about the access restriction:
我也遇到过这个错误,但我的项目是使用 Maven 和tycho编译器(它是一组 OSGi 插件)在命令行上构建的。在大量筛选有相同问题但在 Eclipse 中而不是在命令行中修复它的人之后,我在 Tycho 开发者论坛上发现了一条消息,该消息回答了我的问题,使用配置pom.xml
来忽略有关访问限制的编译器警告:
<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.
更多信息可以在第谷常见问题解答中找到。这花了我 AGES 来解决,所以我想我会通过发布这个答案来帮助其他人尝试从命令行修复这些访问限制错误。
回答by Soumyajit Swain
In my case there was a mismatch between the build path JRE and installed JRE on execution environment. I moved into Project > Properties > Java compiler. There was a warning message at the bottom.
在我的情况下,构建路径 JRE 和执行环境上安装的 JRE 之间存在不匹配。我进入了项目 > 属性 > Java 编译器。底部有一条警告信息。
I clicked on the links 'Installed JRE', 'Execution environment', 'Java build path' and changed the JDK version to 1.7 and the warning disappeared.
我单击了“已安装 JRE”、“执行环境”、“Java 构建路径”链接并将 JDK 版本更改为 1.7,警告消失了。