我正在尝试为我的项目生成一个可运行的 jar 文件,该文件具有 JavaFx gui。 该项目在 eclipse 中运行良好,但是当我尝试运行 jar 时:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
...Caused by: java.lang.NullPointerException: Input stream must not be null
图像的代码如下:
private Image image1 = new Image(this.getClass().getResourceAsStream("../pic/classic/image1.png"));
我需要更改什么才能毫无异常(exception)地运行我的 jar 文件。
感谢您的帮助。
最佳答案
..
在 jar 文件中指定资源名称时无效。根据documentation on resource naming资源路径的每个组成部分都应该是有效的 Java 标识符:..
不是。
要解决此问题,只需指定相对于类路径的绝对资源名称。因此,如果您所在的类(class)位于名为 com.mycompany.myapplication.view
的包中,您将使用
private Image image1 =
new Image(this.getClass().getResourceAsStream("/com/mycompany/myapplication/pic/classic/image1.png"));
关于在 Jar 文件中找不到 JavaFx 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38444852/