Дмитрий
Дмитрий

Reputation: 111

java use .png file in .jar

i have simple application with structure like:

this

And i want to use images(.png) in my program from .jar file (after i will compile it). I have read about getResourceAsStream and how i should to define name of .png file so that it would work after .jar compilation.

But it doesn't work in .jar, it works only when i starts appplication throught IDE(Intellij Idea).
Can someone help me with this?

Details:

ImageView imgY = new ImageView(new Image(this.getClass().getResourceAsStream("Y.png")));
ImageView imgN = new ImageView(new Image(this.getClass().getResourceAsStream("N.png")));

In IDE i have set folder which contain .png-images as Resources (look screenshot)

Sorry, i can't add screenshots - the reputation not enought.
Upd: I forgot to say, that after compilation .jar-file contain my .png files, but when i run it, i gave error:
screen again

Upvotes: 0

Views: 1785

Answers (3)

TylerKVu
TylerKVu

Reputation: 135

@JurgenDeLandsheer's answer worked for me. Trying getting the image through the method:

this.getClass().getResourceAsStream("/Y.png")

Upvotes: 0

Jurgen De Landsheer
Jurgen De Landsheer

Reputation: 92

the second problem, how did you create the jar? (are you using maven, IntelliJ, other)

did you doubleclick it to launch?

is the main class set in the manifest (no, because the manifest is missing)?

can you set English as the language in Winrar before taking a screenshot

Upvotes: 0

Jurgen De Landsheer
Jurgen De Landsheer

Reputation: 92

this.getClass().getResourceAsStream("/Y.png")

the / is important (I could not comment on your comment - needed 50 reputation)

in an IDE like IntelliJ the png is still a file and not included in a jar, this should work both ways

yuo can also use

this.getClass().getClassLoader().getResourceAsStream("Y.png")

Upvotes: 1

Related Questions