Using Icons in a Jar file

Hello,

I am having a problem trying to package my image files into a JAR file. I can successfully run my program with the image folder in the package directory. But, when I put the directory tree into a JAR file, suddenly the program cannot find the images anymore.

I am using the class.getResource("images/image.jpg"); command...

My directory tree is this:

<package>

Class.class

<images>

image.jpg

Again, it works fine until I put it into a JAR file. Why is it having trouble locating the files after that?

Thanks in advance for the help,

Dave

[618 byte] By [Dave_Carta] at [2007-11-27 11:27:38]
# 1

I don't wanna be rude but couldn't you find some documentation on that subject on the internet? http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html

DikkeDouwea at 2007-7-29 16:17:19 > top of Java-index,Desktop,Core GUI APIs...
# 2

Yeah, maybe I'm misunderstanding that page, but I actually copied my method from that site.

I have

protected ImageIcon createImageIcon(String path) {

java.net.URL imgURL = RPSApplet.class.getResource(path);

if (imgURL != null) {

message( ("image url: " + imgURL.toString()) );

return new ImageIcon(imgURL);

} else {

message("Couldn't find file: " + path);

return null;

}

}

The problem is, the method works perfectly when I run it outside of a JAR file, but when I put the images in the file, it fails (even though I try all the configurations listed by sun as "acceptable," the getResource command returns a null pointer.

This is my directory structure in the jar file

<rpsApp> //package directory

-RPSApplet.class//method's class

<images>//image folder

-image files//images

I mean, I may be making a foolish mistake, but I can't spot it....

Dave_Carta at 2007-7-29 16:17:19 > top of Java-index,Desktop,Core GUI APIs...
# 3

couple of things to try

1)

move the image into the same folder as the .java file, so

getResource("images/image.jpg");

becomes

getResource("image.jpg");

2)

if still no luck, from the command prompt, navigate your way to the folder

with the image, do a dir and check image.jpg is exactly the same capitalization

i.e. not image.JPG etc

Michael_Dunna at 2007-7-29 16:17:19 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks. MS Paint saved them as fdalsjdf.JPG Now it works. Thanks!

Dave_Carta at 2007-7-29 16:17:19 > top of Java-index,Desktop,Core GUI APIs...