Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (603)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (651)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Re: Accessing images from a JAR  (Read 1833 times)
0 Members and 1 Guest are viewing this topic.
Online erikd

JGO Ninja

Medals: 16
Projects: 4
Exp: 14 years


« Posted 2003-02-16 12:24:49 »

try to replace

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


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).

Offline JuddMan

Senior Devvie

Medals: 1

Your Ad Here

« Reply #1 - Posted 2003-02-16 12:27:56 »

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

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.

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>(
       at TileBank.addTile(
       at TileBank.loadTiles(
       at TileBank.<init>(

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...

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.

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();
            tempTile = new Tile(tempImage, description, pl, pr, pu, pd, intr, index);
Offline zparticle

Senior Devvie

Thick As A Brick

« Reply #2 - Posted 2003-02-16 12:42:14 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JuddMan

Senior Devvie

Medals: 1

Your Ad Here

« Reply #3 - Posted 2003-02-16 13:18:02 »

this is a really strange thing...

           String titleURL = "/tiles/cloud.gif";
           URL url = TileBank.class.getResource(titleURL);
           Image tempIcon = new ImageIcon(url).getImage();

leads to

Reading tile meta data...
Exception in thread "main" java.lang.NullPointerException
       at javax.swing.ImageIcon.<init>(
       at TileBank.addTile(
       at TileBank.loadTiles(
       at TileBank.<init>(
       at BufferStrat.<init>(
       at BufferStrat.main(

same as before...
Offline JuddMan

Senior Devvie

Medals: 1

Your Ad Here

« Reply #4 - Posted 2003-02-16 14:14:44 »

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.

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


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.
Offline zparticle

Senior Devvie

Thick As A Brick

« Reply #5 - Posted 2003-02-16 16:18:08 »

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.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (11 views)
2015-08-01 03:58:20

Jesse (17 views)
2015-07-29 04:35:27

Riven (38 views)
2015-07-27 16:38:00

Riven (19 views)
2015-07-27 15:35:20

Riven (22 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (32 views)
2015-07-25 00:14:37

BurntPizza (43 views)
2015-07-24 22:06:39

BurntPizza (25 views)
2015-07-24 06:06:53

NoxInc (31 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!