代码如下:

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访问限制

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

问题

有些情况下,你想使用公共非api,比如sun。杂项(你不应该这样做,除非你知道自己在做什么)。在某些情况下,Eclipse并不正确(这就是我的情况,我只想使用javax.smartcardio)。在这种情况下,我们在Eclipse中得到这个错误。

解决方案

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

转到Java项目的属性, 即在“包资源管理器”的项目上下文菜单中选择“属性”。 进入“Java Build Path”,选择“Libraries”。 展开库条目 选择 “访问规则”, “编辑……” “添加……”一个带有相应规则模式的“决议:可访问”。 对我来说是“javax/smartcardio/**”,对你来说可能是“com/apple/eawt/**”。

其他回答

它工作:项目属性-> ProjectFacets ->运行时-> jdk1.8.0_45 ->应用

在eclipse oxygen中添加javafx可访问权限 进入项目>属性> java构建路径> libraries>,然后展开库,双击>访问规则,在那里设置权限 解决方案:无障碍 规则模式:javafx/**

删除现有/配置的系统库: 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

现在等着完成它。

我遇到这个问题是因为与我的项目相关联的项目方面是错误的java版本。

为了解决这个问题,我做了以下工作:

右键单击项目并选择Properties 选择“Project Facets”,将java版本更改为大于1.4的版本。 点击【应用】

这将重新构建您的项目,希望错误将得到解决。

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