Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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  
  Accessing Resources from a JAR  (Read 2779 times)
0 Members and 1 Guest are viewing this topic.
Offline sponge

Senior Newbie




Ek is Java.


« Posted 2004-06-23 17:28:37 »

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??
Offline kevglass

« JGO Spiffy Duke »


Medals: 208
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2004-06-23 17:38:53 »

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

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #2 - Posted 2004-06-24 06:24:25 »

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

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

Senior Newbie




Ek is Java.


« Reply #3 - Posted 2004-06-24 16:09:54 »

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

JGO Coder


Medals: 2


pixels! :x


« Reply #4 - Posted 2004-06-24 21:39:20 »

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

弾幕 ☆ @mahonnaiseblog
Offline sponge

Senior Newbie




Ek is Java.


« Reply #5 - Posted 2004-06-25 16:43:59 »

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

JGO Coder


Medals: 2


pixels! :x


« Reply #6 - Posted 2004-06-25 19:39:12 »

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

Let's say you have this lines of code:

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

弾幕 ☆ @mahonnaiseblog
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #7 - Posted 2004-06-25 22:22:24 »

Quote
To make it worse, it works perfectly when its not JAR'ed, so the image names are definately not wrong.


Not necessarily. The windows file system is case insensitive, while paths inside a jar are case sensitive.

I load images like this
1  
2  
                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 /

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #8 - Posted 2004-06-25 22:34:22 »

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

Offline sponge

Senior Newbie




Ek is Java.


« Reply #9 - Posted 2004-06-26 17:32:25 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #10 - Posted 2004-06-28 05:31:37 »

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

Offline aldacron

Senior Devvie


Medals: 9
Exp: 16 years


Java games rock!


« Reply #11 - Posted 2004-06-28 08:27:35 »

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


And they are now known as myjavaserver.com (and supported by JavaLobby).
Offline sponge

Senior Newbie




Ek is Java.


« Reply #12 - Posted 2004-06-29 16:25:29 »

Thanks for the link. Unfortunately, they aren't accepting new users..
8: Undefined index: online
File: /home/jgo/public_html/Themes/default/Display.template.php (main sub template - eval?)
Line: 161