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.getResourceAsStream(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?
From my testing, with my own application but your POM file, the problem seems to be with what "resource" you're trying to find. For example, you have:
public static final String PATH_TO_IMG = App.class.getResource("/img").toString();
But /img
is not a resource, it's a "package". Whether or not calling getResource
with a path to a package is supposed to work, I'm not sure. It may be undefined behavior. Regardless, once packaged into a run-time image, the code will apparently start to return null
in such cases. Yet your resources are being packaged correctly.
I suspect what you're trying to do is use these static final
fields to do things like:
Image image = new Image(PATH_TO_IMG + "foo.png");
Possibly part of the goal is to avoid a bunch of SomeClass.class.getResource(...)
calls everywhere. If I'm right, then you can still do something similar. Have your constants be the root of the resource name :
public static final String IMG_ROOT = "/img";
And then have a utility method:
public static String getImagePath(String imageName) {
String resource = IMG_ROOT + "/" + imageName;
URL url = App.class.getResource(resource);
if (url == null) {
throw new RuntimeException("could not find resource: " + resource);
}
return url.toString();
}
Which you can then use like so:
Image image = new Image(getImagePath("foo.png"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.