【问题标题】:FXMLLoader.load() throws IllegalAccessErrorFXMLLoader.load() 抛出 IllegalAccessError
【发布时间】:2021-05-08 21:43:12
【问题描述】:

我正在构建 javafx 应用程序,我正在尝试使用 FXMLLoader 加载 .fxml 文件。这样做,它会抛出 IllegalAccessError 没有 FXMLLoader 应用程序运行良好。我正在使用 Maven 工具并包含依赖项:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>15.0.1</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>15.0.1</version>
</dependency>

重写启动方法:

@Override
public void start(Stage primaryStage) throws Exception
{
    System.out.println(getClass().getResource(""));
    URL url = getClass().getResource("fxml/defaultScene.fxml");
    Scene scene = new Scene(FXMLLoader.load(url)); // <- exception occurs here
    ViewController viewController = new ViewController(scene); 

模块信息.java:

module DesktopApp
{
    exports core;
    requires javafx.graphics;
    requires javafx.fxml;
}

例外:

java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelpe
r (in unnamed module @0x67583cb) 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 @0x67583cb

【问题讨论】:

  • 模块信息看起来不完整 - 缺少 requires javafx.controls
  • @kleopatra 这实际上解决了我的问题。如果你愿意,你可以发布答案,我会接受。谢谢你的帮助。
  • 我很惊讶您的代码能够编译。您是否将代码编译为模块?
  • @Slaw 好点 :) maven deps 看起来不错,虽然不匹配模块信息 - 奇怪的是这种不匹配不会在编译时沸腾
  • 我想知道,您是否只引用 FXML 文件中 javafx.controls 模块中的类?

标签: java javafx openjfx


【解决方案1】:

解决方案是将requires javafx.controls 添加到module-info.java。它是可编译的,因为 .java 文件中没有 javafx.controls 包中的引用类,并且由于 fxml 文件中引用了 javafx.controls 中的类而引发了异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2012-07-03
    • 2020-06-29
    • 1970-01-01
    相关资源
    最近更新 更多