0

It's still not finding the FXML Location. I've messed with output Directory and directory and the URL going into FXMLLoader to no avail:

Parent root = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));

Where is this application looking for resources?

Directories:

In:

PROJECT/src/main/java/mypackage/Main.java

PROJECT/src/main/resources/fxml/ui.fxml

Out:

PROJECT/target/AppName-1.0-SNAPSHOT.jar

PROJECT/target/AppName-1.0-SNAPSHOT-shaded.jar

PROJECT/target/classes/myPackage/Main.class

PROJECT/target/fxml/ui.fxml

My POM:

<dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>11.0.2</version>
        </dependency>
    </dependencies>

    <build>

        <plugins>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <outputDirectory>${basedir}/target</outputDirectory>
                    <resources>
                        <resource>
                            <directory>src/main/resources</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/*.fxml</include>
                            </includes>
                        </resource>
                    </resources>
                </configuration>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>



            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>  <!--or <release>10</release>-->
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.potatospy.NewMain</mainClass>
                                </transformer>
                            </transformers>
                            <artifactSet>
                                <excludes>
                                    <exclude>classworlds:classworlds</exclude>
                                    <exclude>junit:junit</exclude>
                                    <exclude>jmock:*</exclude>
                                    <exclude>*:xml-apis</exclude>
                                    <exclude>org.apache.maven:lib:tests</exclude>
                                    <exclude>log4j:log4j:jar:</exclude>
                                </excludes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Community
  • 1
  • 1
chrips
  • 4,996
  • 5
  • 24
  • 48
  • Any reason why you are adding and overriding the `maven-resource-plugin` default behavior? – José Pereda Mar 25 '19 at 10:33
  • @JoséPereda I took that from apache maven resource page. It seemed that I had to specifty my resources directory that way. If I remove the overriding, it still doesnt know the location – chrips Mar 25 '19 at 10:50

1 Answers1

2

For starters, the first issue is with your resource:

Parent root = FXMLLoader.load(getClass().getResource("fxml/ui.fxml"));

If you use getResource you have to use an absolute path:

Parent root = FXMLLoader.load(getClass().getResource("/fxml/ui.fxml"));

There are many questions at StackOverflow about this issue, this answer is a very valid reference.

maven-shade-plugin

Assuming your com.potatospy.NewMain class is a launcher class that doesn't extend Application, and your sources (under src/main/java/) are:

  • com/potatospy/NewMain.java
  • com/potatospy/Main.java
  • com/potatospy/UIController.java

and your resources (under src/main/resources) are:

  • fxml/ui.fxml

If you want to do a shade/fat jar, and you don't modify the default maven-resources-plugin, this will work just using the maven-compile-plugin and maven-shade-plugin plugins from your pom:

mvn clean package

then you can run your app:

java -jar target/AppName-1.0-SNAPSHOT.jar

Note that the default resources plugin is applied, and your files are added to target/classes:

  • target/classes/com/potatospy/NewMain.class
  • target/classes/com/potatospy/Main.class
  • target/classes/com/potatospy/UIController.class
  • target/classes/fxml/ui.fxml

maven-resource-plugin

But if you add the resources plugin as you have done in your pom, when you run it, you will notice that your files are added to:

  • target/classes/com/potatospy/NewMain.class
  • target/classes/com/potatospy/Main.class
  • target/classes/com/potatospy/UIController.class
  • target/fxml/ui.fxml

and when the package is done, the fxml file is not even added to the jar!

If you need to include the resources plugin, you'll need something like this:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <outputDirectory>${basedir}/target/classes</outputDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                   <include>**/*.fxml</include>
                </includes>
            </resource>
        </resources>
    </configuration>
    <executions>
        <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Note that I've added classes to the output directory (so the resources are included into it).

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • I'm very thankful for your information. I think after having solved this Location null issue several times via other threads, one thing remained... I really did not understand why the fixes worked. Anyway, I'll study your answer to learn as much as possible so I can start assisting others with it! – chrips Mar 25 '19 at 11:28