英文:
How to include assets in app deployed with JLink?
问题
我有一个JavaFX的Maven项目,它有以下的文件夹结构:
+ -- src/
+ -- -- java/
+ -- -- -- com/pkg/
+ -- -- -- -- App.java
+ -- -- resources/
+ -- -- -- com/pkg/
+ -- -- -- -- fxml/app.fxml
+ -- -- -- -- css/app.css
+ -- -- -- -- img/img.png
当我需要加载一个FXML文件时,例如:
URL url = getClass().getResource("/com/pkg/fxml/app.fxml");
一切都很顺利,直到我尝试使用JLink来发布我的应用。我运行了JLink的Maven目标,它在/target/image/bin/java
创建了一个可执行文件。我可以运行它,但它很快就会引发LoadException
,因为它找不到一些FXML文件。事实上,我在target/
内找不到任何.fxml
,.css
或.png
文件,因为resources/
不存在。
那么我在这里做错了什么 - 是文件结构还是构建?我该如何解决这个问题?
英文:
I have a JavaFX Maven project that has the following folder structure:
+ -- src/
+ -- -- java/
+ -- -- -- com/pkg/
+ -- -- -- -- App.java
+ -- -- resources/
+ -- -- -- com/pkg/
+ -- -- -- -- fxml/app.fxml
+ -- -- -- -- css/app.css
+ -- -- -- -- img/img.png
When I need to load an FXML file, for example, I do:
URL url = getClass().getResource("/com/pkg/fxml/app.fxml");
Everything was fun and games until I tried to ship my app with JLink. I ran JLink maven goal and it created an executable in /target/image/bin/java
. I can run it, but it soon raises LoadException
because it couldn't find some FXML file. Indeed, I can't find any .fxml
, .css
or .png
file inside target/
, because resources/
is not there.
So what am I doing wrong here - the file structure, or the build? And how can I fix this?
专注分享java语言的经验与见解,让所有开发者获益!
评论