从jar运行时无法加载javafx资源 [英] javafx resource cannot be loaded when running from jar

查看:369
本文介绍了从jar运行时无法加载javafx资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The application runs well when I attempt to run with the IntelliJ IDEA build and run button. But after I have build the project into an executable javafx jar artifact and attempted to run from the commandline with java -jar AppName.jar it shows the following error :

    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行的代码

Here is my code from line 60 to 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可执行文件运行时,事情是否会有所不同?

it seems that the resource is assigning to null on the second ln. Am I doing something wrong with the loading process? Does things work differently when running from a jar executable ?

提前谢谢!

推荐答案

Jar文件系统不支持..的相对路径-它可以在intellij,netbeans和eclipse中使用,因为您可以从本地文件系统中进行加载.

Jar filesystem does NOT support relative paths with .. - it works in intellij, netbeans, eclipse because there you load from then native filesystem.

这篇关于从jar运行时无法加载javafx资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆