从jar运行时无法加载javafx资源
当我尝试使用IntelliJ IDEA build and run按钮运行时,应用程序运行良好。但是,在我将项目构建到可执行的javafx jar工件中并尝试使用从jar运行时无法加载javafx资源,java,intellij-idea,javafx,javafx-2,javafx-8,Java,Intellij Idea,Javafx,Javafx 2,Javafx 8,当我尝试使用IntelliJ IDEA build and run按钮运行时,应用程序运行良好。但是,在我将项目构建到可执行的javafx jar工件中并尝试使用java-jar AppName.jar从命令行运行之后,它显示了以下错误: Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.
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可执行文件运行时,工作方式是否有所不同
提前谢谢 我通过以下资源加载:
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包位于与当前类相关的一个包中吗?Jar文件系统不支持与..-它在intellij、netbeans和eclipse中工作,因为在那里您可以从本机文件系统加载。谢谢,但是您有可能解决这个问题吗?是的,请使用绝对路径my/sample/ui/note_main.fxml和getClass().getClassloader()代替teadThank@tomsontom!对于未来有同样担忧的人:我必须调用
getClass().getClassLoader().getResource(“package/where/the/res/is/location.fxml”)
它似乎是在IDE上工作的。我一到家就试试你的建议!:)