代码如下:

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说:

访问限制:'Application'类型不是API(对必需库'/ library /Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar'的限制)


当前回答

对我有用的是将访问限制包添加到MANIFEST中。MF文件。在Eclipse中,“Access Restricted”错误显示在几个文件的Problems选项卡中。我只是右键点击每个错误,点击“快速修复”,并选择“添加[包]到导入的包”。

其他回答

进入以下设置:

窗口->首选项-> java -编译器-错误/警告-弃用和限制api -禁止引用(访问规则)

设置为“警告”或“忽略”。

我也有同样的问题。当我最初在Eclipse中创建java项目时,我指定了JRE 8。当我进入项目的构建路径并编辑JRE系统库时,选择了Java 8执行环境。当我选择使用“Alernate JRE”(仍然是java 8)时,它为我修复了这个错误。

在Eclipse Mars.2 Release(4.5.2)中: 项目资源管理器->上下文菜单->属性-> JavaBuildPath ->库 选择JRE…然后按编辑:切换到工作区JRE (jdk1.8.0_77)

对我有用。

我遇到了一个不同的问题。在项目-属性-库- JRE库,我有错误的JRE库版本。删除并设置实际的一个,瞧——所有的访问限制…警告消失了。

我有eclipse JRE 8.112,不确定这是否重要,但我所做的是:

右键单击我的项目文件夹 点击属性 单击Java构建路径文件夹 一旦进入,我是在订单和出口 我检查了JRE系统库[jre1.8.0_112] 然后把它移到另一个JRE系统库上面(不确定这是否重要) 然后按确定

这解决了我的问题。