访问限制:“应用程序”类型不是API(对所需库rt.jar的限制)
这是代码:
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);
}
}
这里是错误:
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说:
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')
以及这里给出的答案已经不令人满意,所以我做了自己的研究。
This happened to me as well, and the answers given here already were not satisfying, so I did my own research.
Eclipse具有称为访问限制的机制,以防止您意外使用Eclipse认为不属于公共API的类。通常,Eclipse是正确的,在两个意义上:我们通常不想使用不属于公共API的东西。而Eclipse通常是关于什么是什么,什么不是公共API的一部分。
Eclipse has a mechanism called access restrictions to 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.
现在,您可能会想要使用公共非API,例如 sun.misc
(除非你知道你在做什么,否则你不应该)。而且可能会出现这样的情况:Eclipse并不正确(这就是我所发生的事情,我只想使用 javax.smartcardio
)。在这种情况下,我们在Eclipse中收到此错误。
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.
解决方案是更改访问限制。转到Java项目的属性,即从包资源管理器中的项目的上下文菜单中选择属性。在那里,转到Java构建路径,库选项卡。在那里,展开图书馆条目,选择访问规则,编辑...和添加...一个具有相应规则模式的分辨率:可访问。对于我来说,这是 javax / smartcardio / **
,对于你而言,可能是 com / apple / eawt / **
。
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". There, go to "Java Build Path", tab "Libraries". There, 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/**
".