在 JRE 8 中使用 JavaFX,“访问限制”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22812488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using JavaFX in JRE 8, “Access restriction” error
提问by Lexxicon
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:
在我的新 java 8 项目中尝试使用 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
I'm using Eclipse Kepler with the Eclipse JDT patch for java 8.
我将 Eclipse Kepler 与 Java 8 的 Eclipse JDT 补丁一起使用。
This seems to be an issue related to the fact that JavaFX is not a part of the JavaSE execution environment.
这似乎是一个与 JavaFX 不是 JavaSE 执行环境的一部分有关的问题。
I am now toughly confused as according to http://en.wikipedia.org/wiki/JavaFXjavaFX is a part of the JavaSE. Is it possible that Eclipse is not recognizing that it is a part of the javaSE?
我现在很困惑,因为根据http://en.wikipedia.org/wiki/JavaFXjavaFX 是 JavaSE 的一部分。Eclipse 是否有可能没有意识到它是 javaSE 的一部分?
采纳答案by tomsontom
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!).
从 Eclipse 的角度来看,该错误是完全正确的,因为 JavaFX 来自扩展类路径,并且并非在所有 Java8 VM(例如 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.
如上所述,您可以通过添加访问规则或恕我直言来抑制这些警告,最好的解决方案是安装 e(fx)clipse,它会自动为您执行此操作,此外它还为您提供 JavaFX CSS 和 FXML 工具。
You can grab an all in one package from http://efxclipse.bestsolution.at/install.html
您可以从http://efxclipse.bestsolution.at/install.html获取多合一软件包
回答by Ray
I resolved the problem by removing and readding the JDK to the build path. Don't ask me why this works, though.
我通过删除 JDK 并将其读取到构建路径来解决该问题。不过,不要问我为什么这样做。
回答by Calanais
I've resolved this problem by some careful use of the Eclipse project's build path.
我通过谨慎使用 Eclipse 项目的构建路径解决了这个问题。
- 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.
- 调出应用程序项目的属性并选择“构建路径”部分。
- 在“库”选项卡中将 jre8_0\lib\ext\jfxrt.jar 添加为“外部 JAR 文件”。
- 在“Order/Export”选项卡中,确保此 jfxrt.jar 位于列表中的第一个。
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.
这样做是使 Eclipse 将 jfxrt.jar 视为普通的 3rd 方 JAR 文件。该顺序很重要,因此它优先于 JRE 系统库中的条目。它是相同的实际 JAR 文件,但 Eclipse 以不同的方式看待它。
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.
您可能希望更改任何运行配置,使其不使用 jfxrt.jar 的“导出”版本;但实际上,这不会有什么不同。
I've done this with Java8 and Eclipse Luna; but the principal would I am sure work with any combination of Java and Eclipse.
我已经用 Java8 和 Eclipse Luna 做到了这一点;但我肯定会使用 Java 和 Eclipse 的任何组合。
回答by Thach Van
The easy way is to install e(fx)clipse - a plugin for Eclipse to support JavaFX:
简单的方法是安装 e(fx)clipse - Eclipse 支持 JavaFX 的插件:
- Select Help -> Install New Software
- Click Add button to add the following site:
- Name: efxclipse
- Location: http://download.eclipse.org/efxclipse/updates-released/1.2.0/site
- Click OK
- In the "Work with", select the recently added site "efxclipse"
- Check the checkbox "e(fx)clipse - install" to install all components of this selection
- Move to next steps to finish the installation
- Restart your Eclipse. If it still doesn't recognize JavaFX library, restart it again.
- 选择帮助 -> 安装新软件
- 单击添加按钮添加以下站点:
- 名称: efxclipse
- 位置:http: //download.eclipse.org/efxclipse/updates-released/1.2.0/site
- 单击确定
- 在“Work with”中,选择最近添加的站点“efxclipse”
- 选中复选框“e(fx)clipse - install”以安装此选择的所有组件
- 转到下一步以完成安装
- 重新启动 Eclipse。如果它仍然无法识别 JavaFX 库,请再次重新启动它。
Original information can be found here: https://www.eclipse.org/efxclipse/install.html#for-the-lazy
原始信息可以在这里找到:https: //www.eclipse.org/efxclipse/install.html#for-the-lazy
回答by Natile
Follow these steps:
按着这些次序:
Add the jfxrt.jar (from your installation folder) as an "External JAR file" in the 'Libraries' tab.
In the 'Order/Export' tab, ensure that this jfxrt.jar is first in the list.
在“库”选项卡中添加 jfxrt.jar(来自您的安装文件夹)作为“外部 JAR 文件”。
在“订购/导出”选项卡中,确保此 jfxrt.jar 位于列表中的第一个。
This worked for me.
这对我有用。
回答by Natile
I know this has been answered already, but I find the proposed solutions clunky.
我知道这已经得到了回答,但我发现提出的解决方案很笨拙。
- I don't want to install a plugin just to avoid what I consider to be an invalid error.
- 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.
- 我不想安装插件只是为了避免我认为无效的错误。
- 将 jfxrt.jar 添加为外部 jar 将起作用,但如果您计划进行导出,则是一个坏主意,因为 jfxrt.jar 也将被导出。可能不是你想要的。
Another option
另外一个选项
In 'Mars' release (and possibly earlier versions) you can disable access restrictions errors. Under
在'Mars' 版本(可能还有更早的版本)中,您可以禁用访问限制错误。在下面
Java->Compiler->Errors/Warnings view
Java->编译器->错误/警告视图
and
和
"Deprecated and restricted API"
“已弃用和受限的 API”
you can set "Forbidden reference (access rule)" to ignore.
您可以将“禁止引用(访问规则)”设置为忽略。
回答by James_D
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.
我将在这里再添加一个答案,只是为了提供我认为最简单的方法。在我的 Eclipse 设置中,我安装了 e(fx)clipse,它为此提供了一个修复程序,并提供了许多有用的开发功能,如果您正在编写 JavaFX 应用程序,您几乎肯定会想要这些功能。这可能是最实用的方法。如果由于某种原因您不想要该插件,则此答案中概述的解决方案将以最少的其他副作用解决问题。
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.
正如其他答案中所指出的,默认情况下,Eclipse 不允许访问 jre/lib/ext 目录中 jar 文件中的类,因为不能保证所有 Java 平台上都存在这些类。如果您正在编写 JavaFX 8 应用程序,那么您假设您在一个平台上,jfxrt.jar 在 lib/ext 位置可用。
So the minimalfix 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:
因此,对此的最小修复是允许访问此 jar 文件中的类(并且仅在此 jar 文件中)。为此,右键单击项目并打开项目属性对话框。在左窗格中选择“构建路径”,然后选择“库”选项卡。您将看到一个“JRE 系统库”条目。展开该条目,您将看到一个“访问规则”子条目:
Select the "Access Rules" entry and click "Edit". Click "Add".
选择“访问规则”条目并单击“编辑”。单击“添加”。
Under "Resolution", choose "Accessible", and under "Rule Pattern", enter javafx/**
:
在“分辨率”下,选择“可访问”,然后在“规则模式”下输入javafx/**
:
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.
此设置将允许访问以 开头的任何包中的所有类javafx.
,但将为所有其他类保留 ext 文件夹上的规则,并且在这个意义上是“最小的”。
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.
同样,您可能真正想做的是安装 e(fx)clipse 插件,但据我所知,这是对 Eclipse 设置副作用最小的解决方案。
回答by Stefan
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?).
正如 James_D 已经建议的那样,我在类路径文件中添加了访问规则。我暂时卸载了 e(fx)clipse 插件,因为它有一个 css 验证错误(请参阅如何在 Eclipse Mars 中禁用 css 警告“未知属性”?)。
Here is my entry in the classpath file for the JRE container:
这是我在 JRE 容器的类路径文件中的条目:
<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>
回答by usertest
- go to the build pathof the current project
- 转到当前项目的构建路径
under Libraries
在图书馆下
- select the "JRE System Library[jdk1.8xxx]"
- click edit
- and select either "Workspace default JRE(jdk1.8xx)" ORAlternate JRE
- Click finish
- Click OK
- 选择“ JRE系统库[jdk1.8xxx]”
- 点击编辑
- 并选择“工作区默认 JRE(jdk1.8xx)”或备用 JRE
- 点击完成
- 单击确定
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
注意:确保在 Eclipse / Preferences (NOT the project) / Java / Installed JRE 中,jdk 指向 JDK 文件夹而不是 JRE C:\Program Files\Java\jdk1.8.0_74
回答by Mohd Jahid
Step-1: Navigate to your jfxrt.jar. C:\Program Files\Java\jre1.8.0_111\lib\ext
步骤 1:导航到您的 jfxrt.jar。C:\Program Files\Java\jre1.8.0_111\lib\ext
Step-2 Copy the file jfxrt.jar
Step-2 复制文件 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
第 3 步:转到 Eclipse,创建一个新文件夹,如下所示:[创建文件夹名称 lib 以放置我们的 jfxrt.jar 文件][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
步骤 4:选择 lib 文件夹并按 CTRL+V 粘贴 jfxrt.jar [粘贴您的 jfxrt.jar][1] [1]:https://i.stack.imgur.com/Ljogu.png
Step-5: Right click on jfxrt.jar and set as Build Path.
步骤 5:右键单击 jfxrt.jar 并设置为构建路径。