编程语言
首页 > 编程语言> > JavaFX 11:创建Label时出现IllegalAccessError

JavaFX 11:创建Label时出现IllegalAccessError

作者:互联网

这个问题可能与this one的问题大致相同,但似乎该问题的提问者没有添加足够的信息来收到有用的回复.

我正在尝试使用JDK和JavaFx SDK版本11.0.2运行JavaFx应用程序.

此代码完全按预期工作,生成一个空窗口:

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);
    }
}

但是,如果我尝试向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);
    }
}

它生成的堆栈跟踪如下所示(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$2(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$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(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$11(GtkApplication.java:277)
    ... 1 more
Exception running application sample.Main

我正在Kubuntu上运行最新版本的IntelliJ Idea.我从官方网站下载了Oracle JDK和JavaFX

我把javafx-sdk-11.0.2放在/usr/lib / jvm /目录下,而jdk-11.0.2在同一目录下.

在IntelliJ Idea中,我相信我已正确选择了JDK,并且我已将/usr/lib/jvm/javafx-sdk-11.0.2/lib添加为库.

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

我没有修改任何VM选项.

我该如何解决这个错误?它是JavaFx中的错误吗?

解决方法:

您已经在解释您的问题:

I have not modified any VM options.

由于JavaFX 11不再是JDK的一部分,因此您必须使用here中的JavaFX SDK(正如您所做),或者使用Maven / Gradle从Maven Central检索JavaFX模块.

然后,您需要将SDK添加为库,以便IntelliJ可以找到JavaFX类.

但是一旦你完成了这个,并且鉴于JavaFX jar是模块,你仍然需要做两件事:

>使JavaFX模块可用于模块路径
>定义您添加到项目的模块

根据您的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

(注意-p与–module-path相同)

这解释了为什么你的项目在第一种情况下运行,当你没有添加到场景中的控件时,只有StackPane,属于javafx.graphics module,但在添加Label时失败并显示已发布的异常,属于javafx.controls module.

这已经多次说过了:您需要为项目设置所需的VM选项.

首先阅读https://openjfx.io/openjfx-docs/中的文档,包括IntelliJ doc,IDE的非模块化项目部分.并阅读部分4.添加VM选项.

VM Options

所以点击Run – >编辑配置,并添加:

-p /usr/lib/jvm/javafx-sdk-11.0.2/lib --add-modules javafx.controls

应用并运行问题将得到解决.

标签:java,javafx,intellij-idea,javafx-11
来源: https://codeday.me/bug/20190916/1806506.html