gpt4 book ai didi

java - 将 fxml 文件包含到可执行 jar 中

转载 作者:行者123 更新时间:2023-11-30 02:34:15 25 4
gpt4 key购买 nike

我正在尝试创建一个 jar 文件,但当我尝试运行它时,我收到 java.lang.IllegalStateException: Location is not set.

这就是它在我的代码中加载的方式:

 FXMLLoader loader = new FXMLLoader(Main.class.getResource("/view/MainView.fxml"));

jar 文件是使用 intellij idea 构建的:项目结构 -> 工件

已解决

我已经通过添加

设法用 Maven 解决了这个问题
   <resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
</includes>
</resource>
</resources>

和 maven-assemble-plugin

最佳答案

通过 IDE 构建项目并不是一个好主意。它更改了项目的目录结构,因此 fxml 文件的相对路径不再正确。如果您使用 Maven 构建项目,您应该可以摆脱这个问题。然后将 maven-compiler-plugin 添加到生成的 pom.xml 中。

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
...
</plugin>
...
</plugins>

关于java - 将 fxml 文件包含到可执行 jar 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43513153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com