Home arrow static arrow Java Programming [Archive] - Images from a JAR file
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Images from a JAR file
This topic has 5 replies on 1 page.

Posts:23
Registered: 2/20/04
Images from a JAR file  
Jul 11, 2004 2:42 PM



 
I am trying to read an image "wiki/./image.gif" using the command ImageIO.read(new File("wiki/./image.gif")); This works fine when the *.class file and wiki/./*.gif file are in the same folder, but it throws an exception when they are compressed into a JAR file. How do I read an image from a JAR file?
 

Posts:11,200
Registered: 7/22/99
Re: Images from a JAR file  
Jul 11, 2004 2:58 PM (reply 1 of 5)



 
use the getResource() method: ImageIO.read(getClass().getResource("wiki/./image.gif"));
 

Posts:147
Registered: 26.01.04
Re: Images from a JAR file  
Jul 11, 2004 3:25 PM (reply 2 of 5)



 
you could also use an java.net.URL to the image and load that...
packages are treated as folders.
 

Posts:1,183
Registered: 1/23/02
Re: Images from a JAR file  
Jul 11, 2004 3:27 PM (reply 3 of 5)



 
One gotcha to look out for - your file system might not be case-sensitive (e.g. Windows) but names in JAR are case-sensitive.
 

Posts:23
Registered: 2/20/04
Re: Images from a JAR file  
Jul 11, 2004 5:22 PM (reply 4 of 5)



 
Thanks for your help, but it doesn't entirely solve my problem.
See, I'm making a card game, and have a class Card which should load an image wiki/./cardImages.gif. This image has all 54 cards tiled, so the BufferedImage that holds it should be static. Thus, I loaded the image using ImageIO.read(new File("wiki/./cardImages.gif")); in a static context. However, the getClass() method cannot be used in a static context.

Is there another solution to get around this problem?
 

Posts:1,183
Registered: 1/23/02
Re: Images from a JAR file  
Jul 11, 2004 5:36 PM (reply 5 of 5)



 
In static context you can use "Card.class" in place of "getClass()".
 
This topic has 5 replies on 1 page.