206

Here is the code:

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);
    }
}

here is the error:

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 says this:

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')

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
www139
  • 2,111
  • 3
  • 13
  • 8
  • 5
    Try running this using Java 7. – spoko Aug 09 '14 at 20:44
  • 2
    Check these for relevancy: http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar?rq=1 , http://stackoverflow.com/questions/9266632/access-restriction-is-not-accessible-due-to-restriction-on-required-library?rq=1 – user2864740 Aug 09 '14 at 20:44
  • Not using Xcode (yet) – www139 Aug 09 '14 at 20:58
  • I'm not to familiar to java the guy doesn't give an exact path. What does he mean by: com/sun/xml/internal/** – www139 Aug 09 '14 at 21:01
  • 1
    Could you interpret what path he's talking about? Thank you! In the future I will switch to either xCode or netbeans. The guy above said to try java 7 should I? – www139 Aug 09 '14 at 21:04
  • Are you building this in maven? the file path typically references the path in your local maven repository where the jar is kept. – jordaniac89 Oct 31 '14 at 13:42
  • i had a similar problem when i imported a java 1.8 luna project into mars. made a new project in mars and copied src/ in. all the red goes away. – Ray Tayek Apr 21 '15 at 04:30
  • The "access restriction" error may also happen in Bonita Studio. This is most probably due to a missing dependency for the project (Menu Development -> Manage dependencies -> import...). – Olivier Feb 27 '20 at 09:51

22 Answers22

313

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

Background: Eclipse access restrictions

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.

Problem

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.

Solution

The solution is to change the access restrictions.

  • Go to the properties of your Java project,
    • i.e. by selecting "Properties" from the context menu of the project in the "Package Explorer".
  • Go to "Java Build Path", tab "Libraries".
  • Expand the library entry
  • select
    • "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/**".
Torsten
  • 1,696
  • 2
  • 21
  • 42
Christian Hujer
  • 17,035
  • 5
  • 40
  • 47
  • 34
    I have Eclipse Mars (4.5.0) and there are no access rules in Java Build Path -> Libraries. FWIW there's a bigger hammer (you can turn them all off) in Preferences -> Java -> Compiler -> Errors / Warnings -> Deprecated and Restricted API. – Jonathan Ross Nov 25 '15 at 16:39
  • 4
    I have Eclipse Mars.1 Release (4.5.1). For me both solution worked. The answer from Christian and the comment from Jonathan. In the tab Libraries you have to open the one among the listed "JARs and class folders" to see the "Access rules" belongs to it. – Laszlo Hirdi Jan 13 '16 at 16:28
  • 1
    This does NOT work for me. Eclipse Luna. Tried JDK 1.8, 1.7, added access rules, clean project, the errors keep coming back. Also tried suggestion of @JonathanRoss - access rules for forbidden and discouraged references are ALREADY set to Warning, but I get errors. – Steve Cohen Jan 28 '16 at 16:36
  • 3
    This does NOT work for me. Eclipse Luna. Tried JDK 1.8, 1.7, added access rules, clean project, the errors keep coming back. Also tried suggestion of @JonathanRoss - first at Project Level. There, although Project-level configs for Forbidden/Discouraged APIs were set to Warning, I still got errors. I then tried (as Jonathan suggested) going through preferences for workspace level settings, and THERE, Forbidden APIs were set to Error. Changing to warning there did make problem go away, but project level setting should have overridden, think that is a bug in Eclipse. – Steve Cohen Jan 28 '16 at 16:44
  • 2
    I wasn't allowed to modify access restriction in my case: Eclipse Neon with a maven jar project on jar "jfxrt.jar". – pdem Jul 05 '16 at 13:04
  • What is the nonpublic API? I mean I would think all APIs would be public. I searched google for "private API" but not sure what I found is the same thing. – Max May 12 '17 at 14:47
  • @Max thanks for asking that question. The term non-public API is rubbish, I've updated it to public Non-API. I hope that clarifies. If not, just ask again. – Christian Hujer May 13 '17 at 00:55
  • 3
    There is a much simpler alternative: removing and adding back the JRE System Library, as pointed out in https://stackoverflow.com/a/2174607/222838 – mico Nov 14 '17 at 15:27
  • 1
    @mico That solution only works if the class in question is part of `rt.jar`. If the access restriction happened on a different jar on the classpath, removing and adding back `rt.jar` will not solve the issue. Also, adding back `rt.jar` will grant access to _all_ packages from `rt.jar`, which is usually _not_ what you want Eclipse to do. It would make you lose the warning about accessing `public` Non-API. – Christian Hujer Jan 01 '18 at 07:20
96

I was having the same problem. When I initially created the java project in Eclipse I specified JRE 8. When I went into the project's build path and edited the JRE System Library, the Java 8 execution environment was selected. When I chose to use an "Alernate JRE" (still java 8) it fixed the error for me.

Amber
  • 2,413
  • 1
  • 15
  • 20
  • 2
    I'd preferred a global fix, but it's better than all the other answers that enable restricted API globally. (I don't want to accidentally use sun.* in my javafx application). – Mark Jeronimus Jul 19 '15 at 11:53
  • 2
    Thanks, this cleared all warnings in my project. Eclipse was complaining about all classes that were provided by JavaFX (jfxrt.jar) – Goran Vasic Feb 17 '16 at 19:49
  • 3
    Confirmed...I changed JRE 8 u60 with...JRE 8 u60. Apparently, this rebuild being triggered, as opposed to any other way to do so, has the side-effect of fixing the issue. (For a more detailed explanation on how to change this setting, see Nikhil Mahajan's answer here: http://stackoverflow.com/questions/6881622/point-eclipse-ide-to-use-another-jre-jdk-other-than-one-specified-in-java-home .) – elder elder Apr 13 '16 at 13:08
  • Thank you! With this strategy i was able to sucessfully compile the spark java framework. – Martin Pabst Jul 10 '16 at 14:31
  • 1
    Strangely this worked. I only have one JDK installed. How weird. – WesternGun Feb 16 '17 at 23:10
  • 1
    Anyone know how to apply this automatically in Gradle? Whenever I do a Gradle > Refresh, it just override it back to the `Java 1.8`. – codenamezero Sep 19 '17 at 14:00
76

Adding javafx accessible permission in eclipse oxygen go to project> properties> java build path> libraries> then expand the libraries and double click on> Access rules there you set the permission Resolution : Accessible Rule Pattern : javafx/**

enter image description here

enter image description here

enter image description here

vidyakumargv
  • 953
  • 8
  • 12
23

To begin with (and unrelated), instantiating the Application class by yourself does not seem to be its intended use. From what one can read from its source, you are rather expected to use the static instance returned by getApplication().

Now let's get to the error Eclipse reports. I've ran into a similar issue recently: Access restriction: The method ... is not API (restriction on required project). I called the method in question as a method of an object which inherited that method from a super class. All I had to do was to add the package the super class was in to the packages imported by my plugin.

However, there is a lot of different causes for errors based on "restriction on required project/library". Similar to the problem described above, the type you are using might have dependencies to packages that are not exported by the library or might not be exported itself. In that case you can try to track down the missing packages and export them my yourself, as suggested here, or try Access Rules. Other possible scenarios include:

  • Eclipse wants to keep you from using available packages that are not part of the public Java API (solution 1, 2)
  • Dependencies are satisfied by multiple sources, versions are conflicting etc. (solution 1, 2, 3)
  • Eclipse is using a JRE where a JDK is necessary (which might be the case here, from what your errors say; solution) or JRE/JDK version in project build path is not the right one

This ended up as more like a medley of restriction-related issues than an actual answer. But since restriction on required projects is such a versatile error to be reported, the perfect recipe is probably still to be found.

Community
  • 1
  • 1
J. Katzwinkel
  • 1,923
  • 16
  • 22
21

We had to change our application to build against the JDK 1.8 using Window->Preferences->Java->Installed JREs. However, after changing that, the JRE System Library specified in the Project Explorer was still incorrect. To fix this, right click on "JRE System Library [wrong-jre-here]" and change from Execution environment: to "Workspace Default (yer-default-here)"

enter image description here

Dinsdale
  • 581
  • 7
  • 12
18

I had this problem because the project facet associated with my project was the wrong java version.

To fix this is I did the following:

  1. Right click on the project and select Properties
  2. Select 'Project Facets' and change version of java to something greater than 1.4.
  3. Click [Apply]

This will rebuild your project and hopefully the error will be resolved.

Kiriya Keat
  • 181
  • 1
  • 3
14

It worked: Project Properties -> ProjectFacets -> Runtimes -> jdk1.8.0_45 -> Apply

Kamila O
  • 300
  • 2
  • 8
14

In the Eclipse top menu bar:

Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -> 
Deprecated and restricted API -> Forbidden reference (access rules): -> change to warning
MarianD
  • 13,096
  • 12
  • 42
  • 54
Phil Long
  • 141
  • 1
  • 2
10

If you're having this same issue using Spring Tool Suite:

The Spring Tool Suite's underlying IDE is, in fact, Eclipse. I've gotten this error just now trying to use some com.sun.net classes. To remove these errors and prevent them from popping up in the Eclipse Luna SR1 (4.4.2) platform of STS:

  • Navigate to Project > Properties
  • Expand the Java Compiler heading
  • Click on Errors/Warnings
  • Expand deprecated and restricted API
  • Next to "Forbidden reference (access rules)" select "ignore"
  • Next to "Discouraged reference (access rules)" select "ignore"

You're good to go.

Vee
  • 729
  • 10
  • 27
7

Had the same problem. Here's how I solved it: Go to Package Explorer. Right click on JRE System Library and go to Properties. In the Classpath Container > Select JRE for the project build path select the third option (Workspace default JRE).

Source : https://thenewboston.com/forum/topic.php?id=3917

Nishant_Singh
  • 748
  • 1
  • 6
  • 16
  • 1
    Thanks... using the answer proposed by C Hujer I actually found there were "162 rules (non-modifiable)" for the file jfxrt.jar! – mike rodent May 30 '17 at 11:42
6

We use IBM Rational Application Developer (RAD) and had the same problem.

ErrorMessage:

Access restriction: The type 'JAXWSProperties' is not API (restriction on required library 'C:\IBM\RAD95\jdk\jre\lib\rt.jar')

Solution:

go to java build path and under Library tab, remove JRE System Library. Then again Add Library --> JRE System Library

Jonas_Hess
  • 1,874
  • 1
  • 22
  • 32
5

Go to the following setting:

Window -> Preferences -> Java-Compiler-Errors/Warnings-Deprecated and restricted API-Forbidden reference (access rules)

Set it to Warning or Ignore.

Matt
  • 17,290
  • 7
  • 57
  • 71
cxj
  • 61
  • 1
  • 1
4

In Eclipse Mars.2 Release (4.5.2):
Project Explorer -> Context menu -> Properties -> JavaBuildPath -> Libraries
select JRE... and press Edit: Switch to Workspace JRE (jdk1.8.0_77)

Works for me.

Sven
  • 41
  • 3
4

Even if its old question, for me in Eclipse I just right click on Src folder and properties (Alt+Enter) and the check for the Ignore optional compile problems removed the error.

enter image description here

MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45
3

I have eclipse JRE 8.112 , not sure if that matters but what i did was this:

  1. Right clicked on my projects folder
  2. went down to properties and clicked
  3. clicked on the java build path folder
  4. once inside, I was in the order and export
  5. I checked the JRE System Library [jre1.8.0_112]
  6. then moved it up above the one other JRE system library there (not sure if this mattered)
  7. then pressed ok

This solved my problem.

quasar-light
  • 43
  • 1
  • 10
3

I simply just add e(fx)clipse in eclipse marketplace. Easy and simple

jessie
  • 87
  • 1
  • 10
  • Exactly, despite its description mentionning Java FX 2 and FXML, ef(x)clipse also configures Eclipse so it considers jfxrt.jar as an API. – Balderk Mar 18 '21 at 09:25
3

Remove Existing/Configured System Library: 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.

Currently you are at same location: Eclipse(IDE) -> Project Explorer -> Project Name-> (Option) Build Path -> Configure Build Path -> Java Build Path -> Libraries

Now Add Same System Library Again: Add Library -> JRE System Library -> Workspace default JRE ((For me)jre1.8.0_231) -> Finish -> Apply -> Close.

Now wait to finish it.

iamfnizami
  • 163
  • 1
  • 8
2

What worked for me was adding the access restricted package to the MANIFEST.MF file. In Eclipse, the "Access Restricted" errors showed up in the Problems tab for several files. I just right clicked on each error, clicked "Quick Fix", and chose "Add '[package]' to imported packages".

Jasperan
  • 2,154
  • 1
  • 16
  • 40
1

I'm using eclipse neon 3. I just wanted to use javafx.application.Application, so I followed Christian Hujer's answer above and it worked. Just some tips: the access rules are very similar to the import statement. For me, the access rules I added was "javafx/application/**". Just replace the dot in the import statement with forward slash and that's the rule. Hope that helps.

Tony S
  • 45
  • 1
  • 7
1

I had a little different problem. In Project - Properties - Libraries - JRE library, I had the wrong JRE lib version. Remove and set the actual one, and voila - all Access restriction... warnings are away.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
1

If someone is having this issue only in your CI tool while running maven, what did the trick for me was to explicitly define the execution environment in your MANIFEST.MF.

In my case, I did this by inserting the following line in my OSGi bundle manifest file:

Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Fappaz
  • 3,033
  • 3
  • 28
  • 39
0

I had faced the same error on Eclipse 4.20.0 with JRE 1.8. To get rid of this compilation error, do the following:

Project --> Properties --> Java Compiler --> Errors/Warnings --> Deprecated and Restricted API --> Forbidden Reference (access rules) -->  ignore
Binita Bharati
  • 5,239
  • 1
  • 43
  • 24