所以我正在尝试使用 JavaFX 构建一个应用程序,该应用程序为 GUI 的元素加载图像。
在将应用程序导出为可执行 JAR 之前,像这样加载图像不是问题。
Image image = new Image(getClass().getResourceAsStream("/icons/myimage.png"));
但是,当尝试将应用程序导出为 JAR 时,出现以下错误:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1117)
at javafx.scene.image.Image.<init>(Image.java:701)
at uonsupportdesk.view.LoginView.<init>(LoginView.java:65)
at uonsupportdesk.entrypoint.EntryPoint.start(EntryPoint.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
从 NullPointerException 来看,我的印象是导出为 JAR 后文件结构发生了变化,无法找到指定的路径。
在将应用程序导出为 JAR 后如何适当地加载图像?