JavaFX is part of Java, so why does it prove so difficult to get JavaFX code to compile in Eclipse? You would think there wouldn’t be an issue, but there often is a compile error, “Access Restriction: blah blah is not accessible due to restriction on required library”.
There are essentially three fixes for this JavaFX access restriction error in Eclipse.
JavaFX Access Restriction Fix
The first fix is best suited for times when you have a normal Java project that just happens to have some JavaFX that is causing the access restriction. In this situation you don’t want to do any development, you just want to compile some code that uses JavaFX.
In this case, navigate to the workspace or project Preferences. Under Java Compiler, you will find the Errors/Warnings section.
Search for the Forbidden reference (access rules) and set that to Ignore. Once you apply this change, Eclipse will be forced to recompile all of your workspace projects. It should compile your JavaFX project properly now.
Removing the Forbidden reference rule allows JavaFX to compile, but doesn’t give you access to code completion and javadoc comments for JavaFX classes and methods.
If you are planning to make some quick changes or additions to your JavaFX code, then you need a change that makes autocompletion and comments available to you while your writing your JavaFX related code.
In this case, find your copy of jxrt.jar. It will be in with your JDK or JRE libraries. Just find your Java home and browse around a bit. Oracle has the nasty habit of changing JAR locations, but it shouldn’t be too hard to find.
Copy your jxrt.jar to a folder in your Eclipse project. Then, browse to your project preferences.
Inside the Java Build Path preference dialog, select the Library tab. In there, you will have the option to Add Jars… Add the jxrt.jar to the Java Build Path and Apply.
Now you should have access to auto completion and javadoc comments when you are programming JavaFX applications. This also clears up the “Access Restriction: blah blah is not accessible due to restriction on required library” problem.
e(fx)clipse Fixes Access Restriction
The final solution (boy that sounds ominous) is to install e(fx)clipse. It might be tempting to install e(fx)clipse from the Eclipse Marketplace… However, that version of e(fx)clipse is often out of date.
The best place get a current version of the e(fx)clipse plugin for Eclipse is from the project’s installation page at https://www.eclipse.org/efxclipse/install.html
Currently, you can get JavaFX up-and-running from the e(fx)clipse installation page using the update sites they provide links to.
To quote, “To install the e(fx)clipse tooling into your already existing Eclipse you have to add an Update site. There’s no released yet available so you need to add the release update-site http://download.eclipse.org/efxclipse/updates-released/2.4.0/site and http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/ for the needed xtext dependencies.” However, I recommend checking their installation page for the current update links. They also give links to instructions for installing plugins in Eclipse using update sites.
This final solutions not only solves the JavaFX not accessible due to restriction on required library issue, and give you code completion. You also get access to JavaFX project and code templates for quickly creating JavaFX projects.
The only downside to e(fx)clipse is that if you don’t want yet another plugin cluttering your Eclipse environment, you might want to shy away from it.