0

I am building javafx application in which i am trying to load .fxml file using FXMLLoader. Doing so, it throws IllegalAccessError Without FXMLLoader application runs fine. I am using Maven tool and dependencies are included:

<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>

Overriden start method:

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

module-info.java:

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

Exception:

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
Wortig
  • 963
  • 2
  • 11
  • 37
  • module-info looks incomplete - missing `requires javafx.controls` – kleopatra Feb 04 '21 at 15:03
  • @kleopatra This actually solved my issue. If you want, you can post answer and i will accept it. Thank you for help. – Wortig Feb 04 '21 at 16:03
  • 1
    I'm surprised your code managed to compile. Did you compile your code as a module? – Slaw Feb 04 '21 at 22:28
  • @Slaw good point :) The maven deps seem okay, though not matching the module-info - strange that such mismatch doesn't boil up at compile time – kleopatra Feb 05 '21 at 11:17
  • @Slaw well... i did maven update, then maven clean verify in Eclipse IDE. Then just tried to run with run as java application. – Wortig Feb 05 '21 at 14:32
  • 1
    I wonder, do you only reference classes from the `javafx.controls` module in the FXML file? – Slaw Feb 06 '21 at 06:28
  • @Slaw yeah, they are referenced there. Guess that was Scene Builder job. – Wortig Feb 06 '21 at 09:43
  • I'm wondering if they are _only_ referenced there. As in you don't reference any classes from the `javafx.controls` module in actual source code (i.e. `.java` files). If so, it's possible this is a weird corner case. A combination of needing the controls module at run-time, not referencing any controls in code so you don't "need" it at compile-time, and probably a mixture of `--class-path` and `--module-path` configuration (performed by Maven). – Slaw Feb 06 '21 at 09:49

1 Answers1

0

Solution is adding requires javafx.controls to module-info.java. It was compile-able, because there are no referenced classes from javafx.controls package in .java file and exception was raised because classes from javafx.controls are referenced in fxml file.

Wortig
  • 963
  • 2
  • 11
  • 37