Using JavaFX in JRE 8, “Access restriction” error

Lexxicon picture Lexxicon · Apr 2, 2014 · Viewed 94.6k times · Source

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?

Answer

James_D picture James_D · Aug 18, 2015

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.