86

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?

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
Lexxicon
  • 1,071
  • 1
  • 9
  • 10
  • 2
    possible duplicate of [Access restriction on class due to restriction on required library rt.jar?](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar) – Nate Apr 02 '14 at 13:31
  • 3
    You might be correct, his proposed solution half worked for me. By half worked I mean that if I add the JRE using the 'workspace default' jre(jdk1.8.0) it works, but when using the 'execution environment' JavaSE-1.8(jdk1.8.0) I still have the error. – Lexxicon Apr 02 '14 at 13:37

11 Answers11

139

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.

James_D
  • 177,111
  • 13
  • 247
  • 290
  • Is there also a way to configure this from the pom in maven? – RobAu May 13 '16 at 12:45
  • 1
    Clear cut explanation – xploreraj May 24 '16 at 21:15
  • @RobAu See here for a maven work around: https://stackoverflow.com/questions/32565193/how-to-define-access-rules-for-classpath-entries-in-maven-pom-xml-file-for-eclip – Stefan Jul 11 '16 at 14:02
  • 1
    Simple and Perfect!! :) – Sanket Patel Nov 07 '16 at 22:14
  • I see in one line of your answer that `e(fx)clipse is providing many useful development features ` . A new element `SashPane` which fixes some `SliderPane` problems has been added in e(fx)clipse 2.6 but i really can't understand . Is there any classes we can use from e(fx)clipse? I can't find anything similar . I mean i can't access any class or find something useful that i can use inside the program,as library i mean.Correct me cause i know i am somewhere wrong here ...:) – GOXR3PLUS Jan 03 '17 at 10:22
  • I would have given you +1000 if I could. – Aurasphere Oct 02 '17 at 10:57
68

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

dumbPotato21
  • 5,353
  • 5
  • 19
  • 31
tomsontom
  • 5,658
  • 2
  • 19
  • 20
  • 1
    That makes sense, I didn't realize other vm's didn't support JFX. I think my original question is unanswerable due to it not being an error. Unfortunately I am unable to use your proposed solution due to some politics at work. – Lexxicon Apr 02 '14 at 15:51
  • 3
    You know all of those are Eclipse projects - the all-in-one download is simply a nice feature to get you started fast. You can install from Eclipse.org as well if you want. See http://www.eclipse.org/efxclipse/install.html – tomsontom Apr 02 '14 at 16:09
  • I am aware, but due to internal restrictions I am not allowed to use that. That being said, if you feel this is the best solution to this issue I will mark your answer as being correct. – Lexxicon Apr 02 '14 at 16:27
  • 6
    just something on the side : if you cant even install eclipse-plugins at work there is definitely something wrong. Limiting developers to a specific toolset is equal to limiting his set of skills and quality of work. – specializt Oct 30 '14 at 11:06
19

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

Ray
  • 2,574
  • 1
  • 14
  • 27
  • 1
    I believe the reason it may have worked for you is that when you are adding a jre library to the build path it(eclipse) seems to default to the 'workspace default' jre instead of using an execution environment. As JFX doesn't seem to be a part of the javaSE, the envrionment that I selected, I retained the error. Whereas if you just add the default workspace jre it will no longer have those restrictions. – Lexxicon Apr 02 '14 at 14:13
11

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:
  3. Click OK
  4. In the "Work with", select the recently added site "efxclipse"
  5. Check the checkbox "e(fx)clipse - install" to install all components of this selection
  6. Move to next steps to finish the installation
  7. 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

Thach Van
  • 839
  • 9
  • 11
7

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.

Calanais
  • 1,382
  • 8
  • 10
6
  • 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

usertest
  • 1,658
  • 3
  • 21
  • 34
4

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.

lodewykk
  • 108
  • 4
Natile
  • 163
  • 1
  • 5
  • 18
4

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

3

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.

anand
  • 31
  • 2
2

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 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>
Community
  • 1
  • 1
Stefan
  • 6,227
  • 1
  • 34
  • 75
0

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.

Mohd Jahid
  • 11
  • 1