如何在使用 JLink 部署的应用中包含资源文件?

huangapple 未分类评论67阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2020年8月15日 02:34:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/63418321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定