导出jar文件后,无法在res文件夹中加载图像文件

时间:2019-05-04 01:32:34

标签: java eclipse javafx

因此,我在月食res项目中src下的JavaFX文件夹中,其中包含一些jpg图像资产。

当我在Eclipse中运行应用程序时,可以从这些资产文件加载图像数据。但是,当我将应用程序导出到“可执行jar文件”时,加载图像的行显示以下错误:

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:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:498)
    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:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.NullPointerException <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    at FactoryApp.start(FactoryApp.java:66) // my code
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

我尝试了以下代码:

尝试#1:

Image img = new Image("res/" + name + ".jpg");

尝试#2:

Image img = new Image(getClass().getResource("res/" + name + ".jpg").toExternalForm());

尝试#3:

Image img = new Image(getClass().getClassLoader().getResource("res/" + name + ".jpg").toExternalForm());

我不确定自己在做什么错,因为我只能在每个SO帖子上找到上面的代码。

有人可以告诉我我想念什么吗?

预先感谢

0 个答案:

没有答案