问题描述
我正在尝试创建一个jar文件,但是当我尝试运行它时,我得到 java.lang.IllegalStateException:未设置位置。
i'm trying to create a jar file but when i attempt to run it i'm getting java.lang.IllegalStateException: Location is not set.
这是我在代码中加载它的方式:
This is how it is loaded it in my code:
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/view/MainView.fxml"));
jar文件是使用intellij构建的:项目结构 - >工件
jar file is built using intellij idea : Project Structure -> Artifacts
已解决
我已设法通过添加
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
</includes>
</resource>
</resources>
和maven-assembly-plugin
and maven-assembly-plugin
推荐答案
通过IDE构建项目不是一个好主意。它会更改项目的目录结构,因此您的fxml文件的相对路径不再正确。如果使用Maven构建项目,则应该解决该问题。然后将 maven-compiler-plugin
添加到生成的pom.xml中。
Building your project via your IDE isn't a good idea. It changes the directory structure of your project, so your relative path to the fxml file isn't correct anymore. You should get rid of that problem if you build your project with Maven. Then add the maven-compiler-plugin
to your generated pom.xml.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
...
</plugin>
...
</plugins>
这篇关于将fxml文件包含在可执行jar中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!