当从JAR文件运行时,JavaFX资源无法加载。

3

当我使用IntelliJ IDEA的构建和运行按钮尝试运行应用程序时,它运行良好。但是,当我将项目构建为可执行的JavaFX JAR工件,并尝试使用命令行运行java -jar AppName.jar时,它会显示以下错误:

    Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at main.MainApplication.start(MainApplication.java:62)
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher`.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.access$200(GtkApplication.java:48)
    at com.sun.glass.ui.gtk.GtkApplication$6$1.run(GtkApplication.java:149)
    ... 1 more

这是我从60到63行的代码:

    FXMLLoader fxmlLoader = new FXMLLoader();
    URL resource = getClass().getResource("../ui/note_main.fxml");
    InputStream inputStream = resource.openStream(); //THIS HERE IS THE NULLPOINTEREXCEPTION
    Parent root = fxmlLoader.load(inputStream);

看起来在第二行将资源分配为null。 我在加载过程中做错了什么吗? 当从jar可执行文件运行时,事情是否有所不同?

提前致谢!

2个回答

4

Jar文件系统不支持使用..的相对路径 - 它能够在IntelliJ、NetBeans和Eclipse中工作是因为它们是从本地文件系统加载。


谢谢,但您有可能的解决方案吗? - cyc115
1
使用绝对路径 my/sample/ui/note_main.fxml 和 getClass().getClassloader()。 - tomsontom
谢谢@tomsontom!对于将来有相同疑虑的人:我必须调用getClass().getClassLoader().getResource("package/where/the/res/is/located.fxml") - cyc115

-1
我是这样加载资源的:

FXMLLoader statusbarLoader = new FXMLLoader(
                StatusbarController.class.getResource("/../../../../StatusbarView.fxml"));
        try {
            statusbarPane = (Pane) statusbarLoader.load();
        } catch (IOException e) {
            logger.error(e);
            Starter.terminateApplicationBecauseOfError("Problems loading StatusbarView.fxml");
        }

看起来很相似,除了我的命令中有一个前导的/。你确定你的ui-package位于当前类的上一级包中吗?


虽然从IDE中看起来似乎可以工作。我回家后会尝试你的建议! :) - cyc115

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接