Using JavaFX in JRE 8, “Access restriction” error

JavaEclipseJavafxJava 8

Java Problem Overview


When trying to use javafx related classes in my new java 8 project I get an access restriction error from eclipse. So far the only 'solution' I've been able to find is to tell eclipse to ignore the access restriction, but I am not satisfied with that. An example of the error:

Access restriction: The type Pane is not accessible due to 
restriction on required library C:\Program Files\Java\jre8_0\lib\ext\jfxrt.jar

I'm using Eclipse Kepler with the Eclipse JDT patch for java 8.

This seems to be an issue related to the fact that JavaFX is not a part of the JavaSE execution environment.

I am now toughly confused as according to http://en.wikipedia.org/wiki/JavaFX javaFX is a part of the JavaSE. Is it possible that Eclipse is not recognizing that it is a part of the javaSE?

Java Solutions


Solution 1 - Java

I'm going to add one more answer here, just to provide what I think is the most minimal approach. In my Eclipse setup, I have e(fx)clipse installed, which provides one fix for this, as well as providing many useful development features that you will almost certainly want if you are writing JavaFX applications. This is probably the most practical approach. If for some reason you don't want that plugin, the solution outlined in this answer will fix the problem with the least amount of other side effects.

As pointed out in other answers, Eclipse, by default, disallows access to classes in jar files in the jre/lib/ext directory, as these are not guaranteed to be present on all Java platforms. If you are writing a JavaFX 8 application, you are assuming you are on a platform where jfxrt.jar is available in the lib/ext location.

So the minimal fix for this is to allow access to the classes in this jar file (and only in this jar file). To do this, right-click on the project and bring up the project properties dialog. Select "Build Path" in the left pane, and select the "Libraries" tab. You will see a "JRE System Library" entry. Expand that entry, and you will see an "Access Rules" subentry:

enter image description here

Select the "Access Rules" entry and click "Edit". Click "Add".

enter image description here

Under "Resolution", choose "Accessible", and under "Rule Pattern", enter javafx/**:

enter image description here

Click OK to exit all the dialogs.

This setting will allow access to all the classes in any packages beginning javafx., but will preserve the rule on the ext folder for all other classes, and is "minimal" in that sense.

Again, what you probably really want to do is to install the e(fx)clipse plugin, but to my knowledge this is the solution with the least side effects on your Eclipse setup.

Solution 2 - Java

From the Eclipse Point of view the error is totally correct because JavaFX is coming from the extension classpath and is not available on ALL Java8 VMs (e.g. ibm!).

As outlined you can suppress those warnings by add access-rules or IMHO the best solution is to install e(fx)clipse which does this automatically for you and beside that even provides you tooling for JavaFX CSS and FXML.

You can grab an all in one package from http://efxclipse.bestsolution.at/install.html

Solution 3 - Java

I resolved the problem by removing and readding the JDK to the build path. Don't ask me why this works, though.

Solution 4 - Java

The easy way is to install e(fx)clipse - a plugin for Eclipse to support JavaFX:

  1. Select Help -> Install New Software
  2. Click Add button to add the following site:
  1. Click OK
  2. In the "Work with", select the recently added site "efxclipse"
  3. Check the checkbox "e(fx)clipse - install" to install all components of this selection
  4. Move to next steps to finish the installation
  5. Restart your Eclipse. If it still doesn't recognize JavaFX library, restart it again.

Original information can be found here: https://www.eclipse.org/efxclipse/install.html#for-the-lazy

Solution 5 - Java

I've resolved this problem by some careful use of the Eclipse project's build path.

  • Bring up the properties of the you application project and select the 'Build Path' section.
  • Add the jre8_0\lib\ext\jfxrt.jar as an "External JAR file" in the 'Libraries' tab.
  • In the 'Order/Export' tab ensure that this jfxrt.jar is first in the list.

What this doing is making Eclipse see the jfxrt.jar as just a regular 3rd party JAR file. The order is important so that this takes precedence over the entry in the JRE system library. It's the same actual JAR file, but Eclipse is seeing it differently.

You may wish to alter any run configurations so it doesn't use the 'Exported' version of the jfxrt.jar; however in practical terms this won't make a difference.

I've done this with Java8 and Eclipse Luna; but the principal would I am sure work with any combination of Java and Eclipse.

Solution 6 - Java

  • go to the build path of the current project

under Libraries

  • select the "JRE System Library [jdk1.8xxx]"
  • click edit
  • and select either "Workspace default JRE(jdk1.8xx)" OR Alternate JRE
  • Click finish
  • Click OK

enter image description here

Note: make sure that in Eclipse / Preferences (NOT the project) / Java / Installed JRE ,that the jdk points to the JDK folder not the JRE C:\Program Files\Java\jdk1.8.0_74

enter image description here

Solution 7 - Java

Follow these steps:

  1. Add the jfxrt.jar (from your installation folder) as an "External JAR file" in the 'Libraries' tab.

  2. In the 'Order/Export' tab, ensure that this jfxrt.jar is first in the list.

This worked for me.

Solution 8 - Java

I know this has been answered already, but I find the proposed solutions clunky.

  1. I don't want to install a plugin just to avoid what I consider to be an invalid error.
  2. Adding jfxrt.jar as external jar will work, but is a bad idea if you plan on doing an export because jfxrt.jar will be exported as well. Probably not what you want.

Another option

In 'Mars' release (and possibly earlier versions) you can disable access restrictions errors. Under

Java->Compiler->Errors/Warnings view

and

"Deprecated and restricted API"

you can set "Forbidden reference (access rule)" to ignore.

enter image description here

Solution 9 - Java

I had the same problem. I used James_D 's solution but his exact solution did not work for me. But using **/javafx/** instead of javafx/** solved the problem for me. Hope that helps.

PS: I would post this as a comment under James_D 's solution but I just registered and did not have enough "reputation" to do so.

Solution 10 - Java

As James_D already suggested I added access rules to my classpath file. I uninstalled the e(fx)clipse plugin for the time being because it has a css validation bug (see https://stackoverflow.com/questions/31817620/how-to-disable-css-warning-unknown-property-in-eclipse-mars).

Here is my entry in the classpath file for the JRE container:

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.‌​launcher.StandardVMType/JavaSE-1.8">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
    </attributes> 
    <accessrules>
        <accessrule kind="accessible" pattern="javafx/**"/>
        <accessrule kind="accessible" pattern="com/sun/javafx/**"/>
    </accessrules>
</classpathentry>

Solution 11 - Java

Step-1: Navigate to your jfxrt.jar. C:\Program Files\Java\jre1.8.0_111\lib\ext

Step-2 Copy the file jfxrt.jar

Step-3: Go to Eclipse,Create a new folder like this: [Creating folder name lib to put our jfxrt.jar file][2] [2]: https://i.stack.imgur.com/YsJ5S.png

Step-4: Select the lib folder and press CTRL+V to paste the jfxrt.jar [Paste your jfxrt.jar][1] [1]: https://i.stack.imgur.com/Ljogu.png

Step-5: Right click on jfxrt.jar and set as Build Path.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLexxiconView Question on Stackoverflow
Solution 1 - JavaJames_DView Answer on Stackoverflow
Solution 2 - JavatomsontomView Answer on Stackoverflow
Solution 3 - JavaRayView Answer on Stackoverflow
Solution 4 - JavaThach VanView Answer on Stackoverflow
Solution 5 - JavaCalanaisView Answer on Stackoverflow
Solution 6 - JavausertestView Answer on Stackoverflow
Solution 7 - JavaNatileView Answer on Stackoverflow
Solution 8 - Javauser2426985View Answer on Stackoverflow
Solution 9 - JavaanandView Answer on Stackoverflow
Solution 10 - JavaStefanView Answer on Stackoverflow
Solution 11 - JavaMohd JahidView Answer on Stackoverflow