访问限制:“应用程序”类型不是 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)

日食是这样说的:

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

原文由 www139 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 597
1 个回答

我也遇到过这种情况,这里给出的答案已经不能令人满意了,所以我自己研究了一下。

背景:Eclipse 访问限制

Eclipse 有一种称为 访问限制 的机制,以防止您意外使用 Eclipse 认为不属于公共 API 的类。通常,Eclipse 在这两个方面都是正确的:我们通常不想使用不属于公共 API 的东西。 Eclipse 通常对什么是公共 API 的一部分和什么不是公共 API 的部分是正确的。

问题

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

解决方案

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

  • 转到 Java 项目的属性,
    • 即从“Package Explorer”中项目的上下文菜单中选择“Properties”。
  • 转到“Java 构建路径”,选项卡“库”。
  • 展开库条目
  • 选择
    • “访问规则”,
    • “编辑…”和
    • “添加…”具有相应规则模式的“解决方案:可访问”。对我来说是“ javax/smartcardio/** ”,对你来说可能是“ com/apple/eawt/** ”。

原文由 Christian Hujer 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题