Java 由于所需库的限制而对类的访问限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1089904/
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
提问by Jason S
I get this error message in Eclipse:
我在 Eclipse 中收到此错误消息:
Access restriction: The type DirectoryWalker is not accessible
due to restriction on required library
/Library/Java/Extensions/commons-io-1.4.jar
what does this mean? There's this other SO questionon the same topic but it doesn't seem to apply in this case. I'm not creating a new java class, I'm trying to use one.
这是什么意思?在同一主题上还有另一个 SO 问题,但它似乎不适用于这种情况。我不是在创建一个新的 Java 类,而是在尝试使用一个。
采纳答案by Tom Hawtin - tackline
At a guess another library you are using also requires Apache Commons I/O, but a different version. Having a piece of code (transitively) have access to two versions of the same packages is a little tricky. You can probably arrange it so your code and the other library use the same version. It's all much easier with CLASSPATH! :)
猜测您使用的另一个库也需要 Apache Commons I/O,但版本不同。让一段代码(可传递地)访问相同包的两个版本有点棘手。您可以安排它以便您的代码和其他库使用相同的版本。有了 CLASSPATH,一切都变得容易多了!:)
回答by Ben
For what it's worth, I had this error recently too (different library).
值得一提的是,我最近也遇到了这个错误(不同的库)。
My IDE (MyEclipse) references a certain library as part of the overall project classpath, but I also had a copy of the relevant JAR in my machine's Java extensions folder.
我的 IDE (MyEclipse) 引用了某个库作为整个项目类路径的一部分,但我在机器的 Java 扩展文件夹中也有相关 JAR 的副本。
The resulting clash gave the (unhelpful) error that you've experienced. So for anyone else hitting this error, you might want to consider your classpaths too.
由此产生的冲突导致了您遇到的(无益的)错误。因此,对于遇到此错误的其他人,您可能也需要考虑您的类路径。
回答by Maheen
go to java build path and under Library tab, remove JRE System Library. Then again Add Library --> JRE System Library
转到 java 构建路径并在库选项卡下,删除 JRE 系统库。然后再次添加库 --> JRE 系统库
Worked for me
为我工作
回答by David Carboni
If you are using m2eclipse, I find "Update Project Configuration" fixes it.
如果您使用的是 m2eclipse,我发现“更新项目配置”可以修复它。
Right-click the project, select Maven > Update Project Configurationand give it a few seconds.
右键单击该项目,选择Maven > Update Project Configuration并等待几秒钟。
回答by Michael Eric Oberlin
This is my least-favorite error, I've run into it a few times, and the "restriction on required library" is just dull enough to leave you scratching your head. However, it's easy to fix.
这是我最不喜欢的错误,我遇到过几次,“对所需库的限制”只是乏味到让你摸不着头脑。但是,它很容易修复。
The quickest way (I'm using Helios) is to go to Window->Preferences->Java->Compiler->Errors/Warnings, find and open the "Deprecated and Restricted API" bullet, and set Forbidden and Discouraged Reference to something other than Error.
最快的方法(我正在使用 Helios)是转到 Window->Preferences->Java->Compiler->Errors/Warnings,找到并打开“Deprecated and Restricted API”项目符号,然后将 Forbidden and Discouraged Reference 设置为某事除了错误。
"Warning" is usually best, as something out there in Java land is triggering this and you probably want a footnote on it. @SuppressWarnings and its cousins, 98% of the time, have the same effect on a programmer's personal and social logic as, say, an ether binge might. But that's just my humble analysis.
“警告”通常是最好的,因为 Java 领域中的某些东西正在触发这一点,您可能想要一个脚注。@SuppressWarnings 及其表亲在 98% 的情况下对程序员的个人和社会逻辑产生的影响与以太狂暴可能产生的影响相同。但这只是我卑微的分析。
Best of luck!
祝你好运!
回答by drverboten
I had the same error the way I fixed was setting the default workspace JRE to the same as the project, I had JRE6 for the workspace and JRE7 for the project setting both to JRE7 fixed the problem
我有同样的错误,我修复的方式是将默认工作区 JRE 设置为与项目相同,我将工作区设置为 JRE6,将项目设置为 JRE7 都设置为 JRE7 修复了问题
回答by Hasan Sawan
Project build path settings -> Libraries tab -> select JRE System Library -> Edit button -> change from Execution Environment radio tab which says J2SE 1.x (jdk1.6.0_21) to Workspace default JRE (jdk1.6.0_21) [Collected]
项目构建路径设置 -> 库选项卡 -> 选择 JRE 系统库 -> 编辑按钮 -> 从执行环境单选选项卡更改为 J2SE 1.x (jdk1.6.0_21) 到工作区默认 JRE (jdk1.6.0_21) [收集]
This is the best solution for sure!
这无疑是最好的解决方案!
回答by CMA
In my case, I was able to solve this by unticking the folder attribute 'Read-only' in the Propertiesof the project folder.
就我而言,我可以通过取消勾选“文件夹属性来解决这个只读”的属性项目文件夹中。
Then, I re-configured Build pathof the project, then Clean.
然后,我重新配置了项目的Build 路径,然后是Clean。
It had solved the error.
它已经解决了错误。
回答by Joost
For those looking why Eclipse can build the project, but Maven can't, check if you have JUnit as a Library, or as a plugin dependency. If it is added as a library, Maven will not be able to pick it up and throw many access restriction (type xyz is not API).
对于那些想知道为什么 Eclipse 可以构建项目而 Maven 不能的人,请检查您是否将 JUnit 作为库或作为插件依赖项。如果将其添加为库,Maven 将无法获取它并抛出许多访问限制(类型 xyz 不是 API)。