2

For a school project we have to use JavaFX14, combined with maven. But when I try to run the application I'm getting an error that the FXMLLoader can't access class com.sun.javafx.util.Utils:

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: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$2(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 @0x20841eae) 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 @0x20841eae
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    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(AccessController.java:391)
    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.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application sample.Main

The main class:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

The maven import file:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>IIPSENE</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx</artifactId>
            <version>14.0.1</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>14</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>14.0.1</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>14</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

What are we doing wrong here? We don't want to add javafx as a VM option to the run configuration. The sample.fxml file just contains a gridpane to show an empty screen.

  • How do you run your project? From IDE or with Maven? – José Pereda May 26 '20 at 10:17
  • If you don't want to add VM options you need to invoke javafx plugin's run goal to run your project. – hat May 26 '20 at 10:35
  • 1
    Or you can make the project modular, by including a `module-info.java` file. – James_D May 26 '20 at 12:12
  • @JoséPereda, from the IDE. – Gijs van der Weijden May 26 '20 at 15:42
  • @hat, how exactly do I do that? – Gijs van der Weijden May 26 '20 at 15:43
  • The reason why it fails from IDE is explained [here](https://stackoverflow.com/a/54757193/3956070) (see `javafx.fxml missing` section). You should run as documented [here](https://openjfx.io/openjfx-docs/#IDE-Intellij). – José Pereda May 26 '20 at 15:51
  • @James_D's solution worked! thanks for the help. if you would want to you can add the comment as an answer to the question so I can mark it as solved. – Gijs van der Weijden May 26 '20 at 15:55
  • Adding a `module-info.java` is in general a good solution, of course. But just so you understand the problem, with that module descriptor, the IDE finds the required modules (that's why it works, both IDE/Maven), but without it, the IDE just "guesses" what modules are required and that's why it fails when FXML is used. In this case, only using the Maven plugin works. – José Pereda May 26 '20 at 16:09
  • @JoséPereda, that makes sence. thanks! – Gijs van der Weijden May 26 '20 at 16:12
  • 1
    @GijsvanderWeijden you already have the [javafx Maven plugin](https://github.com/openjfx/javafx-maven-plugin) in your pom. On the command line you would use `mvn javafx:run` or select the correct goal from your IDE (if you are using IntelliJ, from right side panel) – hat May 26 '20 at 17:11

1 Answers1

0

@James_D's solution worked for me.

The solution was to add a module-info.java file to the src folder of my project.

The contents of my module-info file are as follows:

module MODULENAME {
    requires javafx.controls;
    requires javafx.fxml;
    exports PACKAGENAME;
    opens PACKAGENAME to javafx.graphics;
}