Poseydon
Poseydon

Reputation: 69

Can't load file from jar archive

I'm beginer programmer and I tried to write classic Snake game. I use Maven to build my project and work from IntelliJ IDEA. i putted my texture into folder

src\main\resources\com\github\poseydon42\snakegame\assets

It's simple png file named snake_head.png. Then I tryied to load this image from my code, using thisa code

ImageIO.read(new File(getClass().getResource("/com/github/poseydon42/snakegame/assets/snake_head.png").getFile()));

but I have this error:

Exception in thread "main" java.lang.NullPointerException
at com.github.poseydon42.snakegame.Game.<init>(Game.java:22)
at com.github.poseydon42.snakegame.Main.main(Main.java:10)

How I can load this image.

P.S.: It don't work in IDE and in cmd using built jar file.

Upvotes: 0

Views: 109

Answers (1)

StanislavL
StanislavL

Reputation: 57421

Try instead

ImageIO.read(getClass().getResourceAsStream("/com/github/poseydon42/snakegame/assets/snake_head.png")));

Upvotes: 3

Related Questions