创建标签时的JavaFX 11:IllegalAccessError [英] JavaFX 11: IllegalAccessError when creating Label

查看:84
本文介绍了创建标签时的JavaFX 11:IllegalAccessError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能与这个问题相同. a>,但似乎该请求者没有添加足够的信息来获得有用的答复.

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.

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

I am trying to run a JavaFx application with JDK and JavaFx SDK version 11.0.2.

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

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

但是,如果我尝试在StackPane上添加标签,则会引发异常.

However, if I try to add a label to the StackPane, an exception is thrown.

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行是创建标签的位置):

The stack trace it produces looks like this (line 13 in Main is where the Label is created):

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

I am running a recent version of IntelliJ Idea on Kubuntu. I downloaded the Oracle JDK as well as JavaFX, from the official websites

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

I put javafx-sdk-11.0.2 in the directory /usr/lib/jvm/ and jdk-11.0.2 is in the same directory.

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

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正在使用此命令来启动应用程序(为提高可读性而拆分):

IntelliJ Idea is using this command to launch the Application (split up for readability):

/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选项.

I have not modified any VM options.

如何解决此错误?这是JavaFx中的错误吗?

How can I fix this error? Is it a bug in JavaFx?

推荐答案

您已经在说明问题了:

我还没有修改任何VM选项.

I have not modified any VM options.

由于JavaFX 11不再是JDK的一部分,因此您必须使用

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.

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

Then you need to add the SDK as a library, so IntelliJ can find the JavaFX classes.

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

But once you have done that, and given that the JavaFX jars are modules, you still need to do two things:

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

根据您的IntelliJ输出,默认情况下会添加javafx.graphicsjavafx.base:

Based on your IntelliJ output, it is adding by default javafx.graphics and 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相同)

这说明了为什么您的项目在第一种情况下运行的原因,当您没有将控件添加到场景中时,仅属于javafx.graphicsStackPane 模块.

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.

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

This has been said a number of times: you need to set the required VM options for your project.

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

Start by reading the documentation at https://openjfx.io/openjfx-docs/, including the IntelliJ doc, section Non-modular projects for your IDE. And read the part 4. Add VM options.

因此,单击运行->编辑配置,然后添加:

So click on Run -> Edit Configurations, and add:

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

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

Apply, and run, the issue will be solved.

这篇关于创建标签时的JavaFX 11:IllegalAccessError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆