JavaFX 11:创建标签时出现 IllegalAccessError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54291958/
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
JavaFX 11: IllegalAccessError when creating Label
提问by Reinis Mazeiks
This question is likely about the same issue as this one, but it appears the asker of that one hasn't added enough information to receive a helpful response.
这个问题可能是同一个问题的这一个,但现在看来,一个的提问者还没有添加足够的信息来获得一个有用的响应。
I am trying to run a JavaFx application with JDK and JavaFx SDK version 11.0.2.
我正在尝试使用 JDK 和 JavaFx SDK 11.0.2 版运行 JavaFx 应用程序。
This code works exactly as expected, producing an empty window:
此代码完全按预期工作,产生一个空窗口:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 420, 420));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
However, if I try to add a label to the StackPane
, an exception is thrown.
但是,如果我尝试向 中添加标签StackPane
,则会引发异常。
import ...
import javafx.scene.control.Label;
public class Main extends Application {
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
root.getChildren().add(new Label("42"));
primaryStage.setScene(new Scene(root, 420, 420));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The stack trace it produces looks like this (line 13 in Main is where the Label is created):
它生成的堆栈跟踪如下所示(Main 中的第 13 行是创建 Label 的位置):
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0xbbd2743) cannot access class com.sun.javafx.scene.layout.RegionHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene.layout to unnamed module @0xbbd2743
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at javafx.scene.control.Control.<clinit>(Control.java:86)
at sample.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop(GtkApplication.java:277)
... 1 more
Exception running application sample.Main
I am running a recent version of IntelliJ Idea on Kubuntu. I downloaded the Oracle JDK as well as JavaFX, from the official websites
我在 Kubuntu 上运行最新版本的 IntelliJ Idea。我从官方网站下载了 Oracle JDK 和 JavaFX
I put javafx-sdk-11.0.2
in the directory /usr/lib/jvm/
and jdk-11.0.2
is in the same directory.
我放在javafx-sdk-11.0.2
目录中/usr/lib/jvm/
,jdk-11.0.2
并且在同一目录中。
In IntelliJ Idea, I believe I have correctly chosen the JDK, and I have added /usr/lib/jvm/javafx-sdk-11.0.2/lib
as a library.
在IntelliJ Idea中,我相信我已经正确选择了JDK,并且我已将其添加/usr/lib/jvm/javafx-sdk-11.0.2/lib
为库。
IntelliJ Idea is using this command to launch the Application (split up for readability):
IntelliJ Idea 正在使用此命令启动应用程序(拆分以提高可读性):
/usr/lib/jvm/jdk-11.0.2/bin/java
-Djava.library.path=/usr/lib/jvm/javafx-sdk-11.0.2/lib
--add-modules javafx.base,javafx.graphics
--add-reads javafx.base=ALL-UNNAMED
--add-reads javafx.graphics=ALL-UNNAMED
-javaagent:/opt/jetbrains/idea-IU-183.4886.37/lib/idea_rt.jar=36031:/opt/jetbrains/idea-IU-183.4886.37/bin
-Dfile.encoding=UTF-8
-classpath
/home/rm/IdeaProjects/JfxPlayground/out/production/JfxPlayground
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/src.zip
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx-swt.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.web.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.base.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.fxml.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.media.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.swing.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.controls.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.graphics.jar
-p
/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.base.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.graphics.jar
sample.Main
I have not modified any VM options.
我没有修改任何 VM 选项。
How can I fix this error? Is it a bug in JavaFx?
我该如何解决这个错误?它是 JavaFx 中的错误吗?
采纳答案by José Pereda
You are already giving an explanation of your issue:
您已经在解释您的问题:
I have not modified any VM options.
我没有修改任何 VM 选项。
Since JavaFX 11 is not part of the JDK anymore, you have to use the JavaFX SDK (as you are doing) from here, or alternatively use Maven/Gradle to retrieve the JavaFX modules from Maven Central.
由于 JavaFX 11 不再是 JDK 的一部分,因此您必须从这里使用 JavaFX SDK(正如您所做的那样),或者使用 Maven/Gradle 从 Maven Central 检索 JavaFX 模块。
Then you need to add the SDK as a library, so IntelliJ can find the JavaFX classes.
然后需要将 SDK 添加为库,以便 IntelliJ 可以找到 JavaFX 类。
But once you have done that, and given that the JavaFX jars are modules, you still need to do two things:
但是一旦你这样做了,并且考虑到 JavaFX jars 是模块,你仍然需要做两件事:
- Make the JavaFX modules available to your module-path
- Define which modules do you add to the project
- 使 JavaFX 模块可用于您的模块路径
- 定义您将哪些模块添加到项目中
Based on your IntelliJ output, it is adding by default javafx.graphics
and javafx.base
:
根据您的 IntelliJ 输出,它默认添加javafx.graphics
并且javafx.base
:
--add-modules javafx.base,javafx.graphics
-p /usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.base.jar
:/usr/lib/jvm/javafx-sdk-11.0.2/lib/javafx.graphics.jar
(note that -p
is the same as --module-path
)
(注意-p
与 相同--module-path
)
This explains why your project runs in the first case, when you don't have a control added to the scene, just the StackPane
, that belongs to the javafx.graphics
module, but fails with the posted exception when you add the Label
, that belongs to the javafx.controls
module.
这解释了为什么您的项目在第一种情况下运行,当您没有将控件添加到场景中时,只有StackPane
, 属于javafx.graphics
模块,但是当您添加Label
, 属于javafx.controls
模块时会因发布的异常而失败。
This has been said a number of times: you needto set the required VM options for your project.
这已经说过很多次了:您需要为您的项目设置所需的 VM 选项。
Start by reading the documentation at https://openjfx.io/openjfx-docs/, including the IntelliJdoc, section Non-modular projects for your IDE. And read the part 4. Add VM options.
首先阅读https://openjfx.io/openjfx-docs/ 上的文档,包括IntelliJ文档,IDE 的非模块化项目部分。并阅读第 4 部分。添加 VM 选项。
So click on Run -> Edit Configurations, and add:
所以点击 Run -> Edit Configurations,然后添加:
-p /usr/lib/jvm/javafx-sdk-11.0.2/lib --add-modules javafx.controls
Apply, and run, the issue will be solved.
应用,运行,问题就解决了。
回答by u9107233
check your JAVA_HOME, JAVA_HOME'version must be jdk9+ or jdk11+
检查你的JAVA_HOME,JAVA_HOME'版本必须是jdk9+或jdk11+