在 JRE8中使用 JavaFX,“访问限制”错误

当尝试在我的新 Java8项目中使用与 javafx 相关的类时,我从 eclipse 中得到一个访问限制错误。到目前为止,我能找到的唯一“解决方案”是告诉 eclipse 忽略访问限制,但我对此并不满意。 这个错误的一个例子是:

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

我正在使用 EclipseKepler 和用于 java 8的 EclipseJDT 补丁。

这似乎与 JavaFX 不是 JavaSE 执行环境的一部分这一事实有关。

我现在非常困惑,因为根据 http://en.wikipedia.org/wiki/JavaFX,javaFX 是 JavaSE 的一部分。有没有可能 Eclipse 没有意识到它是 javaSE 的一部分?

102424 次浏览

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

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

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.

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

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.

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

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.

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>
  • 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

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.

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.