How to access files in a jar file?

Can anyone tell me how to access a file contained in my jar file?

I have about 15 classes jarred and deployed as an applet. I want to include in the jar file some .gif files to be used as icons in a tree view. Rather than trying to access them locally with all the security headaches this brings, can I not simply jar them as resources and point to them in my code?

Any help much appreciated,

Thanks in advance,

Chris

[455 byte] By [cfoster2] at [2007-9-26 3:58:53]
# 1

Hello Chris,

Please read this note taken directly from Sun, from their Java Tutorial/Creating a GUI with Swing/Using Other Swing Features/How to use Icons/Specifying the Image Source (whew!)

/*

Note: Applets are supposed to be able to load images from JAR files.

Currently, however, some browsers can't read images from a JAR file,

although they do successfully get classes from a JAR file.

With our applets, we currently hedge our bets by both putting

the image files in the applet's archive file (the JAR file containing

the applet's class files) and by putting the image files in the file

system on the server.

*/

So......Do you want to take a chance?

-Ron

rSully at 2007-6-29 12:52:17 > top of Java-index,Desktop,Deploying...
# 2
Use thisInputStream is = some_class.getClassLoader().getResourceAsStream("my_resource");
dudenko at 2007-6-29 12:52:17 > top of Java-index,Desktop,Deploying...
# 3

If you'd like to give it a go, try this:

public ImageIcon getImageIcon(String imageName)

{

URL imgURL = null;

try

{

imgURL = this.getClass().getResource(imageName);

}

catch (Exception e){}

return new ImageIcon(imgURL);

}

-Ron

rSully at 2007-6-29 12:52:17 > top of Java-index,Desktop,Deploying...