代码如下:

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 JRE 8.112,不确定这是否重要,但我所做的是:

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

这解决了我的问题。

其他回答

删除现有/配置的系统库: Eclipse(IDE) -> Project Explorer -> Project Name-> (Option) Build Path -> Configure Build Path -> Java Build Path -> Libraries -> (Select) JRE System Library [(For me)jre1.8.0_231] -> Remove。

目前你在同一位置: Eclipse(IDE) ->项目资源管理器->项目名称->(选项)构建路径->配置构建路径-> Java构建路径->库

现在再次添加相同的系统库: Add Library -> JRE System Library -> Workspace default JRE ((For me)jre1.8.0_231) -> Finish -> Apply -> Close

现在等着完成它。

在Eclipse顶部菜单栏中:

Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -> 
Deprecated and restricted API -> Forbidden reference (access rules): -> change to warning

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

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

Bundle-RequiredExecutionEnvironment: JavaSE-1.8

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

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