Maven can't find FXMLLoader

digitig picture digitig · Nov 18, 2018 · Viewed 8.2k times · Source

I have a JavaFx project in which Maven install keeps giving me the error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project gui: Compilation failure: Compilation failure:
[ERROR] /C:/Users/digit/eclipse-workspace/gsn-notator/gui/src/main/java/gui/AppController.java:[21,19] package javafx.fxml does not exist
[ERROR] /C:/Users/digit/eclipse-workspace/gsn-notator/gui/src/main/java/gui/AppController.java:[29,17] cannot find symbol
[ERROR] symbol:   class FXMLLoader
[ERROR] location: class gui.AppController
[ERROR] /C:/Users/digit/eclipse-workspace/gsn-notator/gui/src/main/java/gui/AppController.java:[29,45] cannot find symbol
[ERROR] symbol:   class FXMLLoader
[ERROR] location: class gui.AppController

What am I doing wrong? How do I persuade Maven/Eclipse/Java/Whatever find the FXMLLoader?

The project structure has two modules, gui and logic, with the project structure in this tutorial. My overall project pom is:

<modules>
    <module>logic</module>
    <module>gui</module>
</modules>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                    <version>6.2</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

The pom for the gui module is:

<parent>
    <groupId>com.gmail.digitig</groupId>
    <artifactId>gsn-notator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>gui</artifactId>
<dependencies>
    <dependency>
        <groupId>com.gmail.digitig</groupId>
        <artifactId>logic</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>  
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11-ea+19</version>
    </dependency>
</dependencies>

I don't have anything in the logic module yet, other than a trivial module_info.java. Class gui.AppController, where the error is being reported, is:

package gui;

import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.BorderPane;

public class AppController extends BorderPane {
public AppController() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("App.fxml"));
    try {
        fxmlLoader.load();
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }
}

}

Answer

Naman picture Naman · Nov 18, 2018

You need to include the dependency for javafx-fxml in your pom.xml:

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

and ensure the module-info.java has a requires directive for javafx.fxml module.

requires javafx.fxml;

Other suggestions: