Loading files (images) from a JAR

Okay, here goes:

I’m trying to load two images from a .jar file that contains two folders (and the META-INF, but that doesn’t matter in this situation), volatileimagetest (where the code is) and images (where the two images are).
When I run the code (below) before it’s packaged into a .jar, it runs just fine. I have two directories, volatileimagetest (where the code is) and images (where the two images are). The directories are exactly the same as in the .jar, but not in a jar file.

Now I saw on http://weblogs.java.net/blog/chet/archive/2004/08/toolkitbuffered.html that to load images from within a .jar file, I should use the code (slightly modified of course):

URL url = getClass().getResource("/images/img1.png");
BufferedImage img = ImageIO.read(url);

This loads the images when running outside the .jar file. I printed url to System.out, and got this: “file:/C:/Documents%20and%20Settings/Owner/My%20Documents/Java/VolatileImage/build/classes/images/img1.png”. I feel I should just say the directory volatileimagetest is located in the same directory as images, and contains all class files.

When I run this as a .jar, I get a NullPointException, and I narrowed it down to the line “URL url = getClass().getResource(”/images/img1.png");", with getResource() returning null.

Has anyone experienced this, and if so could they please help ???

Can you do me a favor? Type "jar -tvf " on the command line and paste the output here. I’d like to see precisely how the JAR is organized. Also, what package is the main class in?

I got:

 0 Tue Jan 10 21:07:06 GMT-07:00 2006 META-INF/

202 Tue Jan 10 21:07:04 GMT-07:00 2006 META-INF/MANIFEST.MF
0 Tue Jan 10 20:43:00 GMT-07:00 2006 images/
0 Tue Jan 10 20:43:00 GMT-07:00 2006 volatileimagetest/
78425 Mon Jan 09 17:05:24 GMT-07:00 2006 images/img1.PNG
238 Mon Jan 09 17:06:06 GMT-07:00 2006 images/img2.PNG
4717 Tue Jan 10 21:07:06 GMT-07:00 2006 volatileimagetest/Form.class
771 Tue Jan 10 21:07:06 GMT-07:00 2006 volatileimagetest/Main.class

Well, “PNG” != “png”.

Always use lowercase extensions.

looks like you are using Windows which is not case sensitive… or maybe a mac… but java is generally case sensitive.

Well, the filesystem is case insensitive on windows (sorta), but jars certainly aren’t (because the OS isn’t involved in the resolving process).

is have another etra question
lets say is hava a main jar
in which i have an images.jar which contains all my images
how can i load those?

No, you dont pack jars inside jars. (That doesnt make any sense)

Well, anyways… you could load a jar inside a jar inside a jar inside a jar… with some ZipInputStream or a JarInputStream, but it doesnt make any sense… mind you.

to keep it cleen?
was just a question anyway

I usually do. Must have been a fluke with the image editing/creation program I was using (it defaults to upper. I usually remember to change it to lower). Thanks for pointing that out. I am running Windows, so I’m used to case-insensitivity. I’ll try changing that when I get to my compiler (at school right now…).

to keep it cleen?

Well, you can either use several jars for different parts (nice for webstart). Or if it needs to be a single jar you can just use directories (inside the jar).