Java 访问限制:“应用程序”类型不是 API(对所需库 rt.jar 的限制)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25222811/
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: The type 'Application' is not API (restriction on required library rt.jar)
提问by www139
Here is the code:
这是代码:
package mscontroller;
import javax.swing.*;
import com.apple.eawt.Application;
public class Main {
public static void main(String[] args)
{
Application app = new Application();
app.setEnabledAboutMenu(true);
AMEListener listener = new AMEListener();
app.addApplicationListener(listener);
JFrame mainFrame = new JFrame("Application Menu Example");
mainFrame.setSize(500, 500);
mainFrame.setVisible(true);
}
}
here is the error:
这是错误:
Exception in thread "main" java.lang.Error: Unresolved compilation
problems: Access restriction: The type 'Application' is not API
(restriction on required library
'/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar')
Access restriction: The constructor 'Application()' is not API
(restriction on required library
'/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar')
Access restriction: The type 'Application' is not API (restriction on
required library
'/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar')
Access restriction: The method
'Application.setEnabledAboutMenu(boolean)' is not API (restriction on
required library
'/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar')
AMEListener cannot be resolved to a type AMEListener cannot be
resolved to a type
at mscontroller.Main.main(Main.java:9)
eclipse says this:
eclipse 是这样说的:
Access restriction: The type 'Application' is not API (restriction on required library '/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar')
访问限制:“应用程序”类型不是 API(对所需库“/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar”的限制)
回答by J. Katzwinkel
To begin with (and unrelated), instantiating the Application
class by yourself does not seem to be its intended use. From what one can read from its source, you are rather expected to use the static instance returned by getApplication()
.
首先(和无关),Application
自己实例化类似乎不是它的预期用途。从可以从其源代码中读取的内容来看,您更应该使用getApplication()
.
Now let's get to the error Eclipse reports. I've ran into a similar issue recently: Access restriction: The method ... is not API (restriction on required project)
. I called the method in question as a method of an object which inherited that method from a super class. All I had to do was to add the package the super class was in to the packages imported by my plugin.
现在让我们来看看 Eclipse 报告的错误。我最近遇到了类似的问题:Access restriction: The method ... is not API (restriction on required project)
. 我将有问题的方法称为从超类继承该方法的对象的方法。我所要做的就是将超类所在的包添加到我的插件导入的包中。
However, there is a lot of different causes for errors based on "restriction on required project/library". Similar to the problem described above, the type you are using might have dependencies to packages that are not exported by the library or might not be exported itself. In that case you can try to track down the missing packages and export them my yourself, as suggested here, or try Access Rules. Other possible scenarios include:
但是,基于“对所需项目/库的限制”,导致错误的原因有很多。与上述问题类似,您使用的类型可能依赖于库未导出的包或可能未导出的包本身。在这种情况下,您可以按照此处的建议尝试查找丢失的包并自行导出它们,或者尝试访问规则。其他可能的情况包括:
- Eclipse wants to keep you from using available packages that are not part of the public Java API (solution 1, 2)
- Dependencies are satisfied by multiple sources, versions are conflicting etc. (solution 1, 2, 3)
- Eclipse is using a JRE where a JDK is necessary (which might be the case here, from what your errors say; solution) or JRE/JDK version in project build path is not the right one
- Eclipse 希望阻止您使用不属于公共 Java API 的可用包(解决方案1、2)
- 多个来源满足依赖关系,版本冲突等(解决方案1、2、3)
- Eclipse 正在使用 JRE,其中 JDK 是必需的(这里可能就是这种情况,根据您的错误所说;解决方案)或项目构建路径中的 JRE/JDK 版本不是正确的
This ended up as more like a medley of restriction-related issues than an actual answer. But since restriction on required projectsis such a versatile error to be reported, the perfect recipe is probably still to be found.
这最终更像是与限制相关的问题的混合体,而不是实际的答案。但是,由于对所需项目的限制是一个要报告的通用错误,因此可能仍有待找到完美的方法。
回答by Amber
I was having the same problem. When I initially created the java project in Eclipse I specified JRE 8. When I went into the project's build path and edited the JRE System Library, the Java 8 execution environment was selected. When I chose to use an "Alernate JRE" (still java 8) it fixed the error for me.
我遇到了同样的问题。我最初在 Eclipse 中创建 java 项目时指定了 JRE 8。当我进入项目的构建路径并编辑 JRE 系统库时,选择了 Java 8 执行环境。当我选择使用“备用 JRE”(仍然是 java 8)时,它为我修复了错误。
回答by Christian Hujer
This happened to me as well, and the answers given here already were not satisfying, so I did my own research.
我也遇到过这种情况,这里给出的答案已经不令人满意了,所以我自己做了研究。
Background: Eclipse access restrictions
背景:Eclipse访问限制
Eclipse has a mechanism called access restrictionsto prevent you from accidentally using classes which Eclipse thinks are not part of the public API. Usually, Eclipse is right about that, in both senses: We usually do not want to use something which is not part of the public API. And Eclipse is usually right about what is and what isn't part of the public API.
Eclipse 有一种称为访问限制的机制,以防止您意外使用 Eclipse 认为不是公共 API 一部分的类。通常,Eclipse 在这方面是正确的,在这两种意义上:我们通常不想使用不属于公共 API 的东西。Eclipse 通常是正确的,什么是公共 API 的一部分,什么不是。
Problem
问题
Now, there can be situations, where you want to use public Non-API, like sun.misc
(you shouldn't, unless you know what you're doing). And there can be situations, where Eclipse is not really right (that's what happened to me, I just wanted to use javax.smartcardio
). In that case, we get this error in Eclipse.
现在,在某些情况下,您可能想要使用公共非 API,例如sun.misc
(除非您知道自己在做什么,否则不应该这样做)。并且可能存在 Eclipse 不太正确的情况(这就是发生在我身上的事情,我只是想使用javax.smartcardio
)。在这种情况下,我们会在 Eclipse 中收到此错误。
Solution
解决方案
The solution is to change the access restrictions.
解决方案是更改访问限制。
- Go to the properties of your Java project,
- i.e. by selecting "Properties" from the context menu of the project in the "Package Explorer".
- Go to "Java Build Path", tab "Libraries".
- Expand the library entry
- select
- "Access rules",
- "Edit..." and
- "Add..." a "Resolution: Accessible" with a corresponding rule pattern.
For me that was "
javax/smartcardio/**
", for you it might instead be "com/apple/eawt/**
".
- 转到您的 Java 项目的属性,
- 即通过从“Package Explorer”中项目的上下文菜单中选择“Properties”。
- 转到“Java 构建路径”,选项卡“库”。
- 展开库条目
- 选择
- "访问规则",
- “编辑...”和
- “添加...”一个带有相应规则模式的“分辨率:可访问”。对我来说是“
javax/smartcardio/**
”,对你来说可能是“com/apple/eawt/**
”。
回答by Kiriya Keat
I had this problem because the project facet associated with my project was the wrong java version.
我遇到这个问题是因为与我的项目相关的项目方面是错误的 Java 版本。
To fix this is I did the following:
为了解决这个问题,我做了以下事情:
- Right click on the project and select Properties
- Select 'Project Facets' and change version of java to something greater than 1.4.
- Click [Apply]
- 右键单击项目并选择属性
- 选择“Project Facets”并将java 版本更改为大于1.4 的版本。
- 点击【申请】
This will rebuild your project and hopefully the error will be resolved.
这将重建您的项目,希望错误将得到解决。
回答by Grandclosing
If you're having this same issue using Spring Tool Suite:
如果您在使用 Spring Tool Suite 时遇到同样的问题:
The Spring Tool Suite's underlying IDE is, in fact, Eclipse. I've gotten this error just now trying to use some com.sun.net
classes. To remove these errors and prevent them from popping up in the Eclipse Luna SR1 (4.4.2) platform of STS:
Spring Tool Suite 的底层 IDE 实际上是 Eclipse。我刚刚在尝试使用某些com.sun.net
类时遇到此错误。要删除这些错误并防止它们在 STS 的 Eclipse Luna SR1 (4.4.2) 平台中弹出:
- Navigate to Project > Properties
- Expand the Java Compiler heading
- Click on Errors/Warnings
- Expand deprecated and restricted API
- Next to "Forbidden reference (access rules)" select "ignore"
- Next to "Discouraged reference (access rules)" select "ignore"
- 导航到项目 > 属性
- 展开 Java 编译器标题
- 单击错误/警告
- 扩展已弃用和受限的 API
- 在“禁止引用(访问规则)”旁边选择“忽略”
- 在“不鼓励的参考(访问规则)”旁边选择“忽略”
You're good to go.
你很高兴去。
回答by Kamila O
It worked: Project Properties -> ProjectFacets -> Runtimes -> jdk1.8.0_45 -> Apply
它有效:项目属性 -> ProjectFacets -> 运行时 -> jdk1.8.0_45 -> 应用
回答by cxj
Go to the following setting:
转到以下设置:
Window -> Preferences -> Java-Compiler-Errors/Warnings-Deprecated and restricted API-Forbidden reference (access rules)
Window -> Preferences -> Java-Compiler-Errors/Warnings-Deprecated and Restricted API-Forbidden 参考(访问规则)
Set it to Warning
or Ignore
.
将其设置为Warning
或Ignore
。
回答by Phil Long
In the Eclipse top menu bar:
在 Eclipse 顶部菜单栏中:
Windows -> Preferences -> Java -> Compiler -> Errors/Warnings ->
Deprecated and restricted API -> Forbidden reference (access rules): -> change to warning
回答by Sven
In Eclipse Mars.2 Release (4.5.2):Project Explorer -> Context menu -> Properties -> JavaBuildPath -> Libraries
select JRE... and press Edit: Switch to Workspace JRE (jdk1.8.0_77)
在 Eclipse Mars.2 Release (4.5.2) 中:Project Explorer -> Context menu -> Properties -> JavaBuildPath -> Libraries
选择 JRE... 并按 Edit:切换到 Workspace JRE (jdk1.8.0_77)
Works for me.
为我工作。
回答by quasar-light
I have eclipse JRE 8.112 , not sure if that matters but what i did was this:
我有 eclipse JRE 8.112 ,不确定这是否重要,但我所做的是:
- Right clicked on my projects folder
- went down to properties and clicked
- clicked on the java build path folder
- once inside, I was in the order and export
- I checked the JRE System Library [jre1.8.0_112]
- then moved it up above the one other JRE system library there (not sure if this mattered)
- then pressed ok
- 右键单击我的项目文件夹
- 转到属性并单击
- 单击 java 构建路径文件夹
- 一旦进入,我在订单和出口
- 我检查了 JRE 系统库 [jre1.8.0_112]
- 然后将它移到另一个 JRE 系统库的上方(不确定这是否重要)
- 然后按确定
This solved my problem.
这解决了我的问题。