在Eclipse中运行我的代码时,界面和功能按预期工作。导出到jar时,如果包含加载图标的注释代码行,则界面将不会显示。
相关代码:
public void start(Stage primaryStage)
{
try
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("Display.fxml"));
Parent root = (Parent)loader.load();
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Bill Pay Refund Generator");
((Controller) loader.getController()).setStage(primaryStage);
//primaryStage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("resources/cc16.png")));
primaryStage.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
包含图像的资源文件夹位于项目bin文件夹中。
这是从命令行运行jar时的堆栈跟踪:
java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1128)
at javafx.scene.image.Image.<init>(Image.java:706)
at application.Main.start(Main.java:36)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
编辑:在捕获异常时,会显示窗口,但不显示图标。我想更普遍的问题是我需要在哪里保存资源图像?