简体   繁体   中英

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.

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

试试吧

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM