访问限制:“应用程序"类型不是 API(对所需库 rt.jar 的限制) [英] Access restriction: The type 'Application' is not API (restriction on required library rt.jar)

查看:27
本文介绍了访问限制:“应用程序"类型不是 API(对所需库 rt.jar 的限制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

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 是这样说的:

eclipse says this:

访问限制:'Application' 类型不是 API(对所需库'/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')

推荐答案

我也遇到了这种情况,这里给出的答案已经不令人满意,所以我自己研究了.

This happened to me as well, and the answers given here already were not satisfying, so I did my own research.

Eclipse 有一种称为访问限制的机制,以防止您意外使用 Eclipse 认为不是公共 API 一部分的类.通常,Eclipse 在这两个方面都是正确的:我们通常不想使用不属于公共 API 的东西.对于公共 API 的一部分和不包括的内容,Eclipse 通常是正确的.

Eclipse has a mechanism called access restrictions to prevent you from accidentally using classes which Eclipse thinks are not part of the public API. Usually, Eclipse is right about that, in both senses: We usually do not want to use something which is not part of the public API. And Eclipse is usually right about what is and what isn't part of the public API.

现在,在某些情况下,您可能想要使用公共非 API,例如 sun.misc(除非您知道自己在做什么,否则不应该这样做).并且可能存在 Eclipse 不太正确的情况(这就是我遇到的情况,我只是想使用 javax.smartcardio).在这种情况下,我们会在 Eclipse 中收到此错误.

Now, there can be situations, where you want to use public Non-API, like sun.misc (you shouldn't, unless you know what you're doing). And there can be situations, where Eclipse is not really right (that's what happened to me, I just wanted to use javax.smartcardio). In that case, we get this error in Eclipse.

解决方案是更改访问限制.

The solution is to change the access restrictions.

  • 转到您的 Java 项目的属性,
    • 即通过从包资源管理器"中项目的上下文菜单中选择属性".
    • 访问规则",
    • 编辑..."和
    • 添加..."一个带有相应规则模式的分辨率:可访问".对我来说是javax/smartcardio/**",对你来说可能是com/apple/eawt/**".
    • "Access rules",
    • "Edit..." and
    • "Add..." a "Resolution: Accessible" with a corresponding rule pattern. For me that was "javax/smartcardio/**", for you it might instead be "com/apple/eawt/**".

    这篇关于访问限制:“应用程序"类型不是 API(对所需库 rt.jar 的限制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆