【问题标题】:Jar file won't run after Build artifact - JavaFX构建工件后 Jar 文件不会运行 - JavaFX
【发布时间】:2017-11-18 13:55:50
【问题描述】:

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

Project Scource

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

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

Building the artifact

堆栈跟踪异常

 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 加载器”

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);}
}

【问题讨论】:

  • "jar 文件无法运行"。实际发生了什么?你有例外吗?如果没有,您是否尝试过从命令行运行 jar 文件以查看它是否以这种方式生成堆栈跟踪?
  • 我实际上在应用程序启动方法中遇到 java.lang.reflect.InvocationTargetException 异常,但程序在 IDE 上完美运行!
  • 所以在问题中发布完整的堆栈跟踪。可能是FXML文件的路径不正确,或者FXML文件没有部署到jar文件中。
  • 我更新了问题并添加了 StackTrace 异常,谢谢!
  • 你不能把它作为文本发布吗?我看不懂。

标签: java intellij-idea javafx jar fxml


【解决方案1】:

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

而不是

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2021-08-15
    • 2021-11-12
    • 2023-03-21
    • 2022-06-11
    • 2021-04-24
    • 2021-10-13
    相关资源
    最近更新 更多