代码如下:

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顶部菜单栏中:

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

其他回答

在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选项卡中。我只是右键点击每个错误,点击“快速修复”,并选择“添加[包]到导入的包”。

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

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

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

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

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