Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

java - JavaFx Image not found in Jar file

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

The code for the images looks like:

private Image image1 = new Image(this.getClass().getResourceAsStream("../pic/classic/image1.png"));

What do I need to change so that i can run my jar file with no exception.

Thanks for the help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...