JavaFX 真的可以在非模块化环境中使用吗?

Can JavaFX realistically be used in a non-modular environment?

我已尝试按照以下说明使用 JavaFX:

https://openjfx.io/openjfx-docs/ 在“JavaFX 和 IntelliJ”下 -> “使用 Maven 的非模块化”

完成第 1 步和第 2 步(安装和验证)后,我尝试 运行 来自 IntelliJ 的一个非常简单的程序,而不是通过 Maven 插件。

TraderWindow.java

public class TraderWindow extends Application {

    @SneakyThrows
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    @SneakyThrows
    public void start(Stage primaryStage) {
        Scene scene = FXMLLoader.load(getClass().getResource("/TraderWindow.fxml"));
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

TraderWindow.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Button mnemonicParsing="false" text="Button" />
   </children>
</GridPane>

然而,当我尝试 运行 时,我得到了:

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:564)
    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:564)
    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:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x6adc19e3) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x6adc19e3
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2138)
    at com.trader.gui.TraderWindow.start(TraderWindow.java:19)
    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:474)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:446)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
    ... 1 more
Exception running application com.trader.gui.TraderWindow

Process finished with exit code 1

相关部分:

Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x6adc19e3) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x6adc19e3

我想这是可以理解的,因为 JafaFx 是模块化的,而我的应用程序不是。为了避免这个问题,我将其添加到 VM 路径:

--add-opens javafx.graphics/com.sun.javafx.util=ALL-UNNAMED

我又试了一次,这次得到:

Caused by: java.lang.IllegalAccessError: class javafx.fxml.JavaFXBuilderFactory (in unnamed module @0x4e342850) cannot access class com.sun.javafx.reflect.ConstructorUtil (in module javafx.base) because module javafx.base does not export com.sun.javafx.reflect to unnamed module @0x4e342850

所以我添加了另一个 --add-opens 指令...然后我在放弃之前又重复了这些步骤 3 次。

是否有另一种方法使 JavaFX 与 Java 11+ 一起工作,而不是模块化应用程序或为每个非法访问操作煞费苦心地添加 --add-opens 指令?

如果你完全想摆脱所有这些模块系统的麻烦,你可以在你的 main class

中添加这样一行
class MyAppLauncher {public static void main(String[] args) {MyApp.main(args);}}

然后通过此启动器启动应用程序。如果这样做,您可以将所有内容放在 class 路径上并删除所有 -add-opens。 (实际上你必须这样做。)唯一的缺点是,从 JavaFX 16 开始,你会收到一条令人讨厌的警告消息,上面写着 Unsupported JavaFX configuration 但你可以忽略它。