小编典典

访问限制:“Application”类型不是API(对所需库rt.jar的限制)

java

这是代码:

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 says this:

访问限制:类型“应用程序”不是API(对所需库“ /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar”的限制)


阅读 650

收藏
2020-03-13

共1个答案

小编典典

这也发生在我身上,这里给出的答案已经不令人满意,所以我做了自己的研究。

背景:Eclipse 访问限制
Eclipse具有一种称为访问限制的机制,可防止你意外使用Eclipse认为不属于公共API的类。通常,从两种意义上讲,Eclipse都是正确的:我们通常不想使用不属于公共API的内容。Eclipse通常对公共API的组成部分是正确的是正确的。

问题
现在,在某些情况下,你想使用公共Non-API,例如sun.misc(你不应该,除非你知道自己在做什么)。在某些情况下,Eclipse并不是真的正确(这就是发生在我身上的事情,我只是想使用javax.smartcardio)。在这种情况下,我们会在Eclipse中收到此错误。


解决方案是更改访问限制。

  • 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/”.
2020-03-13