Jar 文件不会 运行 在构建工件后 - JavaFX

Jar file won't run after Build artifact - JavaFX

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

Project Scource

该程序在 IDE“IntelliJ”上完美运行 但是当我尝试制作并构建工件时,jar 文件不会 运行“当然安装了 JRE”

我尝试了很多方法来构建工件,那么是否有特定的方法来构建包含 FXML 文件的 jar 文件?

Building the artifact

StackTrace 异常

 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$launchApplication5(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$launchApplication12(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(WinApplication.java:191)
        ... 1 more
Exception running application sample.Main

Main Class 包含这个“FXML Loader”

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("/Sample/MainLayout.fxml"));

            primaryStage.setTitle("Estimated Download Time");
            primaryStage.setScene(new Scene(root, 764, 600));
            primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("Logo.png")));
            primaryStage.resizableProperty().setValue(Boolean.FALSE);
            primaryStage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {launch(args);}
}

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

而不是

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

我删除了包名"Sample"所以变成了这个

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