Another dumb Jar question thats magic!

I have an app that I want to jar with its resources.

I have a dir structure such as this -


images/fruit/apple.png
............/orange.png
............/xxx.png

images/plants/tree.png
............./shrub.png
............./xxx.png

com/app/MainApp.class
......./xxx.class

The problem is I don’t know how many images will be in each dir. Even if I did, I’m not going to hand code all the names, that would be silly. If I know ahead of time the specific filename, I can already load that from the jar file.

How do I ‘parse’ or examine the jar file to get the names out? I don’t have a seperate jar, everything is in one jar.

Help, please!

Thanks,
Dr. A>

Here you go…

http://java.sun.com/developer/JDCTechTips/2004/tt0727.html#2

Thats mostly what I need, except the jar is the one I’m running in, so I don’t neccesarily have its name.

Errr… This is sorrta weird to explain. If I take all my files and then jar them up, I have a single jar. Now run the jar file by doing a -

java -jar myProgram.jar

Inside that jar is where I want to list the contents of the myProgram.jar.

Does that make more sense?

Dr. A>

You could get an URL of one of your classes, for example:
URL url = Yourmain.class.getResource("/package/Yourmain.class")
… and then ask for the full path of the JAR with some of the many URL.methods (maybe use some regexp on the result to get just the file name).

Well, you could generate lists of the directory content. Load it and load the images specified in that list.

On windows you would generate a list like this:
-open the command line
-change to the drive (eg: x:)
-change to the directory (eg: cd blacd moo)
-run the dir command with the bare switch and redirect the output to a text file (eg: dir/b *>list.txt)

I have mostly gotten this to work. :slight_smile: I can post the code if anyone is interested. Its biggest help is when you don’t know what resources you have in the jar (images, sounds, etc).

A weird thing is that if you run code from a jar its url will start -

file:/c:/stuff/more/application.jar!/MyClass.class

(Linux would just drop the c: part)

If you get the URL when you are running from JWS as a jar you lose the first /. I’ve no idea why.

If anyone would like me to post the code, let me know.

As an aside, here’s why I don’t ‘know’ what resources I have.

Imagine you are writing an app which alters photos. So you have a dir which contains photos of all kinds. You want to be able to just drop a new photo in the directory and have your program load it up.

If you want the convenience and flexability of not having to hard code your image names, you need some way of figuring out what they are at runtime. If you are running from inside a jar file, then your images are now inside the jar with you as well, so you can use the normal File() classes to determine what you have. You need to figure out where ‘you’ are, then open up your jar and locate the jar entries, which are dirs when you extract them. You get the idea.

Regards,
Dr. A>