Re: Accessing images from a JAR

try to replace

tempIcon = new ImageIcon(getClass().getResource("tiles/cloud.gif")).getImage();

with

tempIcon = new ImageIcon(getClass().getResource("/tiles/cloud.gif")).getImage();

This works for me, although my images are in a separate jar (but are in a directory).

oh. thanks for replying so soon… what, within 20 minutes :slight_smile:

anyway, i deleted the post before i saw your reply.

here’s the original post… and i thought i had fixed it but it didn’t work. your suggestion didnt work either unfortunately.

[quote]hi. i am trying to work out how to access many gif files after they are put into the jar… the gifs are in their own ‘directory’ in the jar file…
eg. using
tempIcon = new ImageIcon(getClass().getResource("cloud.gif"))
allows me to access the cloud file from the root of the jar file, but when i try putting the cloud.gif image into a folder in the jar and use
tempIcon = new ImageIcon(getClass().getResource("tiles/qblock.gif"))
it throws
Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:138) at TileBank.addTile(TileBank.java:92) at TileBank.loadTiles(TileBank.java:40) at TileBank.<init>(TileBank.java:26)

this is after compiling the jar with
jar cvfm BufferStrat.jar MANIFEST.MF *.class *.gif *.java tiles

extracting the jar with WinRar i can run the class file just fine and it loads the images.

code is here… but you have to extract it to run it…
http://members.austarmetro.com.au/~juddman/java/BufferStratBroken.jar

if anyone knows another way to access image files in the jar the program is running from, plz help out. i dont really want to have to dump all the gifs in amongst the class files.

Relevant


public void addTile(String filename, String description,
      boolean pl, boolean pr, boolean pu, boolean pd, boolean intr, int index)
      {
            //tempIcon = new ImageIcon(getClass().getResource(filename)).getImage();
            tempIcon = new ImageIcon(getClass().getResource("tiles/cloud.gif")).getImage();
            tempImage = gc.createCompatibleImage(32,32,Transparency.BITMASK);
            Graphics2D imgGfx = (Graphics2D)tempImage.getGraphics();
            imgGfx.setComposite(AlphaComposite.Src);
            imgGfx.drawImage(tempIcon,0,0,null);
            imgGfx.dispose();
            tempTile = new Tile(tempImage, description, pl, pr, pu, pd, intr, index);
            tiles[index]=tempTile;
      }

[/quote]

This works for me:


public static final String titleURL = "/images/title.png";
URL url = Main.class.getResource(titleURL);
Image titleImage = new ImageIcon(url).getImage();

I just happen to be using my Main class as the reference but it could be any class. The images directory is at the root of the jar file.

this is a really strange thing…

`
System.out.println(“1”);
String titleURL = “/tiles/cloud.gif”;
System.out.println(“2”);
URL url = TileBank.class.getResource(titleURL);
System.out.println(“3”);
Image tempIcon = new ImageIcon(url).getImage();
System.out.println(“4”);

` leads to

Reading tile meta data... 1 2 3 Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:138) at TileBank.addTile(TileBank.java:99) at TileBank.loadTiles(TileBank.java:41) at TileBank.<init>(TileBank.java:27) at BufferStrat.<init>(BufferStrat.java:62) at BufferStrat.main(BufferStrat.java:248)
same as before…

thanks zparticle. your way did work

i had another problem as well with trying to read the “meta.dat” text file from the same location. not quite as easy as reading an image file.

after ages of API surfing and looking at little demos and stuff i worked that out too.

http://www.tair.freeservers.com/Java/readtext.htm

it’s meant for reading an online file… seems though that the jar files are very network orientated.

edit:

Windows and DOS are sooo lenient on file names. they’re not case sensitive and you can have spaces following them. i discovered somehow that all the file names but one had spaces after them in the dat file so it didnt work either way.

good thing MS introduced the intercnangable \ and / things or there’d be yet another annoying compatibility issue to overcome. some of my GIF’s had upper case first letters.

it finally works independently from a jar file.
the result is an extendable cache system which dictates what tiles are available and what they do. that’d let me change it very quickly later on, or add more tiles.

a little more interesting than before, there’re 42 tiles, adding tiles will throw 20 random ones in there. only very slightly lower performance than the BufferStrat demo.

http://members.austarmetro.com.au/~juddman/java/MultiGFXBuffer.jar

That’s funny, this is the first thing I did with your original code. Made it load 50 different images, thought it might make a difference but it still worked fine.