File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Listing files in a JAR?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Listing files in a JAR??" Watch "Listing files in a JAR??" New topic

Listing files in a JAR??

David Kromek

Joined: Feb 27, 2005
Posts: 2

I have a couple of queries:

I have an executable .jar file that contains a single directory called "App". This directory contains the entire application including a number of subdirectories, one of which is called "images". I want to obtain a list of all the files contained in "images", but I'm not quite sure how to do this. I'm aware that ClassLoader.getSystemResource("App/images/" + filename) will return an URL to any particular file in the jar. But I've tried using ClassLoader.getSystemResources() (which is supposed to return an enumeration) without success. I've tried ClassLoader.getSystemResources("App/images/*") but this doesn't return anything. What am I doing wrong?

Also, my java program loads about 250 jpeg files as ImageIcons. The total amount of memory these files occupy is just under 2.5 MB. But I find that I need to set the java executor's heap memory to about 60 MB (using the switch -Xmx60MB) in order to load these files without getting a number of out of memory exceptions. Why do I need to do this? Why is java so resource hungry?

Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
I tried solving your problem of getting the names of all the files in a
certain directory in a jar file by using the java.util.jar package.

The solutions is not very elegant. The program opens the jar file, checks if the given directory really exists and really is a directory and then does a string comparision to see which entries are located in the given directory.

Hope it will guide you towards solving your problem.

[ March 24, 2005: Message edited by: J Isberg ]
I agree. Here's the link:
subject: Listing files in a JAR??
It's not a secret anymore!