我正在尝试使用Maven和Visual Studio Code使我的JavaFx应用程序可执行。
在这个主题上花费了一些时间后,我发现了一些提到jlink的帖子。
对于打包Java/JavaFX应用程序,我还是一个新手,所以我尝试了一下。
目前,我至少可以执行打包的启动器。
但是,在启动应用程序后立即抛出NullPointerException: 无法调用"Object.toString()",因为"java.lang.Class.getResource(String)"的返回值为null。
为了样式化视图组件,我创建了一些.css文件,并将它们放在/style目录中。根据示例JavaFx应用程序的要求,我将此目录放置在Maven创建的/resources目录中。我以类似的方式处理声音和图像文件。
这里您可以看到我的目录结构摘录。
|
|--src/main
| |
| |-- java
| | | ...
| |
| |-- resources
| |
| |-- img
| | | ...
| |
| |-- style
| | | ...
| |
| |-- sound
| | ...
|
|-- target
|
|-- classes
| | ...
| |
| |-- img
| | | ...
| |
| |-- style
| | | ...
| |
| |-- sound
| | | ...
|
|-- ...
|
|-- app
|
|-- bin
|-- ...
现在我正在尝试从我的应用程序中访问资源。
这是我的第一种方法。在从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();
但是在运行jlink之后,我的应用程序崩溃了,显示了前面提到的NullPointerException。
这是我的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>
这是我一直在使用的创建包的命令。
mvn javafx:jlink -f pom.xml
有人知道如何在运行jlink后获取我的样式表、图片和声音的路径吗?路径绝对足够了,我不需要文件本身。
是否有选项将资源复制到特定位置?
jlink
之前的工作状态应该在jlink
之后也“只要正常工作”。看起来您的资源正在错误地打包。您可以展示您的pom.xml
文件以及您用于创建自定义运行时镜像的确切命令(一步一步的)吗? - Slaw