Accessing Resources from a JAR

Ok I’m trying to get my game webstartable (blah…) and I’ve been struggling to get it to find the resources (images and sound). Do I need to use packages for it to work? I’ve tried with and without without any luck. To load my image I use
Toolkit.getDefaultToolkit().getImage(getClass().getResource(“images/imagename.gif”));

The images are in the correct directory. It works when I run it without JAR’ing it.
It throws a NullPointerException when trying to load the image (at sun.awt.image.URLImageSource.getConnection).
My manifest only conatins the main-class line. what classpath should I put in? if any? thanks…
I tried Kevin Glass’s walkthru with webstart/jnlp, didnt help much, any help??

Sorry the walkthrough didn’t help,

Toolkit.getDefaultToolkit().getImage(getClass().getResource(“images/imag ename.gif”));

Should be alright I think. The NPE just means it can’t find the file so you’re probably right about the classpath. Is everything getting packaged into the same jar or multiple?

Kev

Shouldn’t you have a / at the beginning of the path?
Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/imag ename.gif"));

Ok after messing around with it for awhile I narrowed it down. Still the same problem tho. But it only happens with some images (my background images to be exact). I let the program continue to load the others after the exception and all the others load fine.To make it worse, it works perfectly when its not JAR’ed, so the image names are definately not wrong. The only difference i can think of is that those files were created with MS Paint (dont laugh) while the others where created with photoshop, and therefore they’re opague. They’re also considerably larger (640x480), but that shouldn’t make any difference in finding the file, where the exception is taking place? I’ll try re-saving the images with photoshop, for what it’s worth.
Thanks for the help so far…

Also be sure that all extensions are lowercase (in your source as well as file system wise).

My bad. none of the images are loading correctly. The jar file was in the same directory as the classes so it was loading them from the originals. Oops. Anyway, that means I’m back to square one, with no images loading correctly. But I dont understand why it’s even trying to load them from outside the JAR. Everything is getting packaged into the same JAR, tried multiple with no luck so I went back, the ‘/’ in front of the filename messes it up worse and all extensions are lower case :-/ .Anybody else got any ideas? I have a feeling this is one of those really dumb errors that always seem to take the most time to fix.

Well… just print out the url and check if it makes sense.

Let’s say you have this lines of code:


keyboardImagePath = (new Object()).getClass().getResource("/gfx/keyboard.gif");
System.out.println("path: "+keyboardImagePath);

And the jar itself is in “C:\dls\java\q3tools\scancode” and the jar is called “KeyMatrix.jar”.

The output is then:
path: jar:file:/C:/dls/java/q3tools/scancode/KeyMatrix.jar!/gfx/keyboard.gif

And that works perfectly. The jar is really there and the file “keyboard.gif” is really inside the jar inside a sub directory called “gfx”.

So just print the path and take a close look. It should start with “jar:file:/”, then the full path to the jar, then an exclamation mark and finally the relative path to the file you are looking for.

[quote]To make it worse, it works perfectly when its not JAR’ed, so the image names are definately not wrong.
[/quote]
Not necessarily. The windows file system is case insensitive, while paths inside a jar are case sensitive.

I load images like this


                InputStream is = getClass().getResourceAsStream(path);
                image = (BufferedImage) ImageIO.read(is);


where the path variable is a String with a path to the image, starting with a /

BTW, I usually start my jars with
java -cp game.jar my.package.MainClass
instead of
java -jar game.jar
There’s probably no difference, but who knows (I vaguely remember an issue with starting a jar using the -jar option related to multiple jars so I sticked with putting jars in the classpath)…

Ok so I finally got it to work (don’t ask me how, I did so many things I dont know which one made any difference) and I sign it, and create a jnlp file. Ok then while uploading the JAR to my website i start editing my post ‘your games here’ to say that it’s webstartable. And then I find out jnlp files aren’t allowed on my web host.

Aaaaaaargh!!!

so I had to edit my post back again.
Anyway, thanks for the help. Anybody know a good free host? :wink:

I use www.mycgiserver.com. They support jnlp and server side java stuff.

[quote]I use www.mycgiserver.com. They support jnlp and server side java stuff.
[/quote]
And they are now known as myjavaserver.com (and supported by JavaLobby).

Thanks for the link. Unfortunately, they aren’t accepting new users…

Haha wow Im having the same problem sponge, loading images from inside my jar. My application works flawlessly when not in a JAR but just wont display the images when jarred. I remember this happend to me a long time ago and I somehow fixed it, but now I cant remember what I did and I need to do it again.

Ok I found what I was doing wrong, I needed to make a resource anchor because supposedly JWS mangles names etc. Anyone wondering how, read this
http://www.vamphq.com/tutorial.html