javafx resource cannot be loaded when running from jar

陌路散爱 提交于 2020-02-24 09:23:05

问题


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

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

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 ?

Thanks in advance !


回答1:


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




回答2:


I'm loading by ressources like this:

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

Looks similay except the leading / in my command. Are you sure your ui-package is located one package about your current class?



来源:https://stackoverflow.com/questions/24841062/javafx-resource-cannot-be-loaded-when-running-from-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!