用JavaFX加载图像图标会导致jar文件不显示
在Eclipse中运行代码时,界面和功能按预期工作。当导出到jar时,如果包含加载图标的注释代码行,则接口将不会显示 相关代码:用JavaFX加载图像图标会导致jar文件不显示,java,eclipse,javafx,jar,interface,Java,Eclipse,Javafx,Jar,Interface,在Eclipse中运行代码时,界面和功能按预期工作。当导出到jar时,如果包含加载图标的注释代码行,则接口将不会显示 相关代码: public void start(Stage primaryStage) { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("Display.fxml")); Parent root = (Parent)loader.load();
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)
java.lang.NullPointerException:输入流不能为null
位于javafx.scene.image.image.validateInputStream(image.java:1128)
在javafx.scene.image.image.(image.java:706)
at application.Main.start(Main.java:36)
在com.sun.javafx.application.launchempl.lambda$launchApplication1$161(launchempl.java:863)
位于com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
位于com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
位于java.security.AccessController.doPrivileged(本机方法)
com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95)
在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法)
位于com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
位于java.lang.Thread.run(未知源)
编辑:捕获异常时,将显示窗口,但不显示图标。我想更一般的问题是,我到底需要在哪里保存资源映像?
getClass().getResource(“Display.fxml”)
在与类相同的包中查找Display.fxml。Display.fxml工作正常。只有在加载图像图标时才有问题。哦,对了,我错了;问题在于ClassLoader.getSystemResourceAsStream(“resources/cc16.png”)
。resources
目录是否与Display.fxml位于同一位置?在我的bin
文件夹中,我有两个文件夹。一个是应用程序
,另一个是资源
。fxml文件位于应用程序文件夹内,图片位于资源文件夹内。从技术上讲,.fxml文件是一种资源。因为您可以成功读取该文件,所以我会将图像移动到相同的位置,然后让您的代码使用getClass().getResourceAsStream(“cc16.png”)
检索它。或者,您可以将resources
目录添加到项目的打包目录列表中(在这种情况下,Display.fxml可能应该移动到resources中)。getClass().getResource(“Display.fxml”)
在与类相同的包中查找Display.fxml。Display.fxml工作正常。只有在加载图像图标时才有问题。哦,对了,我错了;问题在于ClassLoader.getSystemResourceAsStream(“resources/cc16.png”)
。resources
目录是否与Display.fxml位于同一位置?在我的bin
文件夹中,我有两个文件夹。一个是应用程序
,另一个是资源
。fxml文件位于应用程序文件夹内,图片位于资源文件夹内。从技术上讲,.fxml文件是一种资源。因为您可以成功读取该文件,所以我会将图像移动到相同的位置,然后让您的代码使用getClass().getResourceAsStream(“cc16.png”)
检索它。或者,您可以将resources
目录添加到项目的打包目录列表中(在这种情况下,Display.fxml可能应该移动到resources中)。