Jar文件赢得';t在构建工件之后运行-JavaFX

Jar文件赢得';t在构建工件之后运行-JavaFX,java,intellij-idea,javafx,jar,fxml,Java,Intellij Idea,Javafx,Jar,Fxml,我有一个JavaFx项目,其中包含FXML文件及其控制器和主类 该程序在IDE“IntelliJ”上运行良好 但是,当我试图制作一个工件并构建它时,jar文件就不会运行“当然安装了JRE” 我尝试了很多方法来构建工件,那么有没有一种特定的方法来构建包含FXML文件的jar文件呢 堆栈跟踪异常 Exception in Application start method java.lang.reflect.InvocationTargetException at sun.ref

我有一个JavaFx项目,其中包含FXML文件及其控制器和主类

该程序在IDE“IntelliJ”上运行良好 但是,当我试图制作一个工件并构建它时,jar文件就不会运行“当然安装了JRE”

我尝试了很多方法来构建工件,那么有没有一种特定的方法来构建包含FXML文件的jar文件呢

堆栈跟踪异常

 Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at sample.Main.start(Main.java:20)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        ... 1 more
Exception running application sample.Main
主类包含此“FXML加载程序”


我通过从FXML加载程序中删除包名解决了这个问题

而不是

root = FXMLLoader.load(getClass().getResource("/Sample/MainLayout.fxml"));
我删除了包名“Sample”,所以它变成了这个

 root = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));

“jar文件就是不运行”。到底发生了什么?你有例外吗?如果没有,您是否尝试过从命令行运行jar文件以查看它是否以这种方式生成堆栈跟踪?实际上,我在Application start方法中获得了java.lang.reflect.InvocationTargetException异常,但该程序在IDE上运行得非常好!因此,在问题中发布完整的堆栈跟踪。可能是FXML文件的路径不正确,或者FXML文件没有部署到jar文件。我更新了问题并添加了StackTrace异常,谢谢!你不能把它作为文本发布吗?我不可能读到那个。
 root = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));