从 jar 运行时无法加载 javafx 资源

标签 java intellij-idea javafx javafx-2 javafx-8

当我尝试使用 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);

似乎资源在第二个 ln 上分配给 null。我在加载过程中做错了什么吗?从 jar 可执行文件运行时,事情会有所不同吗?

提前致谢!

最佳答案

Jar 文件系统不支持带有 .. 的相对路径 - 它在 intellij、netbeans、eclipse 中工作,因为你从那里加载本地文件系统。

关于从 jar 运行时无法加载 javafx 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24841062/

相关文章:

java - 在首选项页面中获取应用按钮 + swt eclipse

java - 如何在 Blackberry 的 Canvas 上旋转带有文本的圆圈

java - 来自多个集合的组合算法

spring-boot - IntelliJ 2018.1 Spring Boot 集成测试问题

javafx - 使用自定义 asString 方法将 simpleStringProperty 绑定(bind)到 simpleIntegerProperty

java - 如何从迭代器中获取 int[]?

java - 如何排除 IntelliJ 中的不安全删除?

java - Apache Spark 无法找到类

JavaFX ListView - 删除单元格之间的间距/填充/边距

JavaFX+Afterburner.fx从子窗口访问父窗口