代码如下:

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


当前回答

如果你在使用Spring工具套件时有同样的问题:

Spring工具套件的底层IDE实际上是Eclipse。我刚刚在尝试使用一些com.sun.net类时得到了这个错误。要删除这些错误并防止它们在STS的Eclipse Luna SR1(4.4.2)平台中弹出:

导航到项目>属性 展开Java Compiler标题 点击错误/警告 扩展已弃用和受限的API 在“禁止引用(访问规则)”旁边选择“忽略” 在“不鼓励引用(访问规则)”旁边选择“忽略”

你可以出发了。

其他回答

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

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

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

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

如果你在使用Spring工具套件时有同样的问题:

Spring工具套件的底层IDE实际上是Eclipse。我刚刚在尝试使用一些com.sun.net类时得到了这个错误。要删除这些错误并防止它们在STS的Eclipse Luna SR1(4.4.2)平台中弹出:

导航到项目>属性 展开Java Compiler标题 点击错误/警告 扩展已弃用和受限的API 在“禁止引用(访问规则)”旁边选择“忽略” 在“不鼓励引用(访问规则)”旁边选择“忽略”

你可以出发了。

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

对我有用。

我们不得不改变我们的应用程序,使用windows ->首选项->Java->安装的jre来构建JDK 1.8。但是,在更改后,在项目资源管理器中指定的JRE系统库仍然不正确。要解决这个问题,右键单击“JRE系统库[wrong-jre-here]”,并将执行环境:更改为“工作区默认(yer-default-here)”

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

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