代码如下:

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'的限制)


当前回答

我在Eclipse 4.20.0和JRE 1.8上也遇到过同样的错误。要消除此编译错误,请执行以下操作:

Project --> Properties --> Java Compiler --> Errors/Warnings --> Deprecated and Restricted API --> Forbidden Reference (access rules) -->  ignore

其他回答

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

我只是简单地在eclipse市场中添加e(fx)clipse。简单明了

即使它的老问题,对我来说,在Eclipse中,我只是右键单击Src文件夹和属性(Alt+Enter),检查忽略可选编译问题消除了错误。

如果有人在运行maven时只在您的CI工具中遇到这个问题,那么对我来说,诀窍是在您的MANIFEST.MF中显式地定义执行环境。

在我的例子中,我通过在我的OSGi包清单文件中插入以下行来做到这一点:

Bundle-RequiredExecutionEnvironment: JavaSE-1.8

我用的是日食霓虹灯。我只想使用javafx.application。应用,所以我遵循Christian Hujer上面的答案,它起作用了。只是一些提示:访问规则与import语句非常相似。对我来说,我添加的访问规则是“javafx/application/**”。只需将import语句中的点替换为正斜杠,这就是规则。希望这能有所帮助。