问题描述
我正在尝试生成一个包含JavaFx gui的项目的可运行jar文件。
项目在eclipse中运行greate但是当我尝试运行jar时:
I'm trying to generate a runnable jar file of my project which has a JavaFx gui.The project runs greate in eclipse but whenI try to run the 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"));
我需要更改什么才能运行我的jar文件,无异常。
What do I need to change so that i can run my jar file with no exception.
感谢您的帮助。
推荐答案
。 。
在jar文件中指定资源名称时无效。根据每个组件资源路径应该是有效的Java标识符: ..
不是。
The ..
is not valid in specifying a resource name in a jar file. According to the documentation on resource naming each component of the resource path should be a valid Java identifier: ..
is not.
要解决此问题,只需指定相对于类路径的绝对资源名称。因此,如果您所在的类位于名为 com.mycompany.myapplication.view
的包中,您将使用
To fix this, just specify the absolute resource name, relative to the classpath. So if the class you are in is in a package called com.mycompany.myapplication.view
, you would use
private Image image1 =
new Image(this.getClass().getResourceAsStream("/com/mycompany/myapplication/pic/classic/image1.png"));
这篇关于Jar文件中找不到JavaFx图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!