Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using png in a jar file

Tags:

java

file

jar

png

When I run my jar file, i get a javax.imageio.IIOException: Can't read input file!

But the file is in the jar!

the code:

try {imgs.put("player1" , ImageIO.read(new File("/car1.png")));}
    catch (IOException e)   {System.out.println(e);}

I have tried to put car1.png everywhere in the jar file but its not working.

like image 701
Faleidel Avatar asked Dec 29 '25 03:12

Faleidel


1 Answers

You probably want to do this instead.

InputStream is = getClass().getResourceAsStream("car1.png");
ImageIO.read(is); 
like image 125
CoolBeans Avatar answered Dec 30 '25 16:12

CoolBeans