5

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

}

Naman
  • 27,789
  • 26
  • 218
  • 353
digitig
  • 1,989
  • 3
  • 25
  • 45
  • 1
    Few suggestions: 1. You can [upgrade to the `maven-compiler-plugin:3.8.0`](https://stackoverflow.com/questions/49398894/unable-to-compile-simple-java-10-java-11-project-with-maven/51586202#51586202) 2. You can [upgrade to `javafx-controls:11.0.1`](https://search.maven.org/artifact/org.openjfx/javafx-controls/11.0.1/jar) 3. Official sample for [running JavaFX with Maven](https://openjfx.io/openjfx-docs/#maven) – Naman Nov 18 '18 at 01:55
  • Neither of the upgrades makes any difference. I have tried to align my project with the official sample, though I'm not entirely sure how to distribute it between the top level and the module. Still no change to the error - it can't find FXMLLoader. And the official sample doesn't include FXML. I had no trouble getting JavaFx apps to work with Maven until I tried to include FXML. – digitig Nov 18 '18 at 21:23

1 Answers1

15

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:

José Pereda
  • 44,311
  • 7
  • 104
  • 132
Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    Fails as before, but with an additional error: "package javafx.fxml does not exist". – digitig Nov 18 '18 at 20:02
  • @digitig I could confirm that the dependency and the module do exist and the same code as yours works for me with `mvn install`. Could you verify the configurations of maven in use? Just in case samples help, here is the [maven module in one of my projects on GitHub](https://github.com/namannigam/zealous-codetroops-libraries/tree/master/javafx) and the [`AppController` class same as yours](https://github.com/namannigam/zealous-codetroops-libraries/blob/master/javafx/src/main/java/com/sample/AppController.java) – Naman Nov 19 '18 at 01:46