javamavenvisual-studio-codejavafxjlink

How to get a resources path after jlink-ing?


I am trying to make my JavaFx-Application executable using Maven and Visual Studio Code.

After some time spent on this topic, I found some posts mentioning jlink.

I am a newcomer when it comes to packaging Java/JavaFX applications, so I gave it a try.

Currently, I can at least execute the launcher for the package.

But immediately after starting the application, a NullPointerException is thrown: Cannot invoke "Object.toString()" because the return value of "java.lang.Class.getResource(String)" is null.

For styling the components of my view I created some .css-files and put them inside a /style directory. This directory I placed this, according to the sample JavaFx application, inside a /resources directory created by Maven. In a similar manner, I proceeded with my sound and image files.

Here you can see an excerpt of my directory structure.

|
|--src/main
|  |
|  |-- java
|  |   | ...
|  |
|  |-- resources
|      |
|      |-- img
|      |   | ...    
|      |
|      |-- style
|      |   | ...
|      |
|      |-- sound
|          | ...
|
|-- target
    |
    |-- classes
    |   | ...
    |   |
    |   |-- img
    |   |   | ...
    |   |
    |   |-- style
    |   |   | ...
    |   |
    |   |-- sound
    |   |   | ...
    |
    |-- ...
    |
    |-- app
        |
        |-- bin
        |-- ...

Now I am trying to access my resources from within my application.

This was my first approach. It works just fine when running from VSCode.

    public static final String PATH_TO_STYLESHEET = App.class.getResource("/style").toString();
    public static final String PATH_TO_IMG = App.class.getResource("/img").toString();
    public static final String PATH_TO_SOUNDS = App.class.getResource("/sounds").toString();

But after running jlink, my application crashes, showing the NullPointerException mentioned earlier.

Here is my pom.xml:

<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/maven-v4_0_0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.openjfx</groupId>
    <artifactId>App</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>19</maven.compiler.release>
        <javafx.version>19</javafx.version>
        <javafx.maven.plugin.version>0.0.8</javafx.maven.plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>${maven.compiler.release}</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>${javafx.maven.plugin.version}</version>
                <configuration>
                    <release>${maven.compiler.release}</release>
                    <jlinkImageName>App</jlinkImageName>
                    <launcher>launcher</launcher>
                    <mainClass>com.test.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    
</project>

And this is the command I have been using for creating the package.

mvn javafx:jlink -f pom.xml

Does anyone have an idea how I can get the path to my stylesheets, images, and sounds, after running jlink? The path is absolutely sufficient. I do not need a file itself.

Is there an option to copy the resources to a specific location?


Solution

  • Problem

    You have code such as the following:

    public static final String PATH_TO_IMG = App.class.getResource("/img").toString();
    

    This is trying to get the resource "/img". But according to your question, that is not a resource per se, but instead a directory (i.e., a package). And the problem appears to be the inconsistent behavior of Class#getResource(String) when the String argument denotes a directory. When your code is not in a JRT image then the call to #getResource(String) will return a URL; when your code is packaged in a JRT image then the same call will return null, despite the fact the directory exists.

    I don't know if this behavior is a bug or simply undefined. One interesting thing is ModuleReader#find(String) clearly is capable of finding directories:

    Finds a resource, returning a URI to the resource in the module.

    If the module reader can determine that the name locates a directory then the resulting URI will end with a slash ('/').

    That indicates, to me at least, that what you're trying to do should be possible. But even that method fails when the module is packaged in a JRT image (by returning an empty Optional). Note that if you query the ModuleReader#list() method it will include directories when the module is not in a JRT image, but those same directories are not included when the module is in a JRT image.

    Example

    I've put a minimal example demonstrating this problem at the end of this answer.


    A Solution

    I assume you're using these constants (e.g., PATH_TO_IMG) to do stuff like the following:

    Image image = new Image(PATH_TO_IMG + "foo.png");
    

    Which avoids having calls to SomeClass.class.getResource("...").toString() everywhere. If this is your goal, then I can think of at least one solution. Change your constants to simply reference the resource root. For example:

    public static final String IMG_ROOT = "/img";
    

    Then create a utility method to resolve the resource:

    public static String getImagePath(String name) {
        var resource = IMG_ROOT + "/" + name;
        var url = App.class.getResource(resource);
        if (url == null) {
            throw new RuntimeException("could not find resource: " + resource);
        }
        return url.toString();
    }
    

    And then you can use that utility method like so:

    Image image = new Image(getImagePath("foo.png"));
    

    Possible Alternative

    Another option might be to make use of the JRT FileSystem implementation. Something like the following:

    FileSystem jrtFs = FileSystems.getFileSystem(URI.create("jrt:/"));
    Path path = jrtFs.getPath("modules", "<module-name>", "img");
    // Note: Doesn't seem to include the trailing '/'
    String pathToImg = path.toUri().toString();
    

    Though you'll have to detect if your code is in a JRT image or not.


    Minimal Example

    Given this doesn't have to do with JavaFX specifically, I've created a minimal example to demonstrate this problem.

    Source Code

    Project structure:

    |   pom.xml
    |
    \---src
        \---main
            +---java
            |   |   module-info.java
            |   |
            |   \---sample
            |           Main.java
            |
            \---resources
                \---data
                        file.txt
    

    pom.xml:

    <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/maven-v4_0_0.xsd">
        
        <modelVersion>4.0.0</modelVersion>
        <groupId>sample</groupId>
        <artifactId>app</artifactId>
        <version>1.0-SNAPSHOT</version>
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.release>19</maven.compiler.release>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.10.1</version>
                </plugin>
                
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.3.0</version>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.3.0</version>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jlink-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <launcher>app=app/sample.Main</launcher>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    module-info.java:

    module app {}
    

    Main.java:

    package sample;
    
    public class Main {
    
        public static void main(String[] args) {
            var modRef = Main.class.getModule()
                .getLayer()
                .configuration()
                .findModule(Main.class.getModule().getName())
                .orElseThrow()
                .reference();
            System.out.printf("Module Location = %s%n%n", modRef.location().orElseThrow());
    
            var dataUrl = Main.class.getResource("/data");
            var fileUrl = Main.class.getResource("/data/file.txt");
            System.out.printf("Data URL = %s%nFile URL = %s%n%n", dataUrl, fileUrl);
        }
    }
    

    Building

    I ran these two commands to build the project:

    1. mvn compile jar:jar
    2. mvn jlink:jlink

    For whatever reason, doing mvn compile jar:jar jlink:jlink caused the jlink task to fail.

    Output

    And here is the different output for the different packaging:

    Exploded module:

    ...> java -p target\classes -m app/sample.Main
    Module Location = file:///C:/Users/***/Desktop/jlink-tests/target/classes/
    
    Data URL = file:/C:/Users/***/Desktop/jlink-tests/target/classes/data/
    File URL = file:/C:/Users/***/Desktop/jlink-tests/target/classes/data/file.txt
    

    Modular JAR:

    ...> java -p target\app-1.0-SNAPSHOT.jar -m app/sample.Main
    Module Location = file:///C:/Users/***/Desktop/jlink-tests/target/app-1.0-SNAPSHOT.jar
    
    Data URL = jar:file:///C:/Users/***/Desktop/jlink-tests/target/app-1.0-SNAPSHOT.jar!/data/
    File URL = jar:file:///C:/Users/***/Desktop/jlink-tests/target/app-1.0-SNAPSHOT.jar!/data/file.txt
    

    JRT Image:

    ...> .\target\maven-jlink\default\bin\app
    Module Location = jrt:/app
    
    Data URL = null
    File URL = jrt:/app/data/file.txt
    

    Results

    As you can see, the call to getResource("/data/file.txt") worked every time, but the call to getResource("/data") did not work for the JRT-packaged version.