Listing files inside a JAR

Hi all. Not sure where else to post this… so this seemed the best place.

Anyway. What I need to to do here is list files from a directory called “images” inside a JAR file. Whenever I didn’t have my application in a JAR, I used the following:

File list[] = (new File("images")).listFiles(filter);

(filter is my FileFilter to return only PNG images)

However, I can’t use new File(“images”) in a JAR because it would just think I’m trying to access the directory outside the JAR. So I need to somehow create a File from this URL:

URL dir = Thread.currentThread().getContextClassLoader().getResource("images");

And I obviously need a File object because nothing else has the method listFiles() (that I know of). I’ve spent the last hour searching and trying everything I know. I even tried using the following:

URL dir = Thread.currentThread().getContextClassLoader().getResource("images");
File list[] = (new File(URI.create(dir.toString()))).listFiles(filter);

But it tells me that it’s not hierarchical. What can I do here?

Thanks,
Jamison


URL dir = Thread.currentThread().getContextClassLoader().getResource("images");
File file = new File(dir.toURI());

Enjoy :slight_smile:

I tried that. But what I just realized here is that toURI() was not introduced until Java 1.5… and I’m compiling with 1.4 (however I have the 1.5 and 1.6 JRE) but I also have the Java 1.6 JDK too.

OK, so anyway. I got 1.5 JDK now. I compile it, no errors, except depreciation warnings (why?). But I get a runtime error:

#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d1279f7, pid=5504, tid=2360
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_10-b03 mixed mode, sharing)
# Problematic frame:
# C  [awt.dll+0xb79f7]
#
# An error report file with more information is saved as hs_err_pid5504.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

Why? You build the JAR file, so you already know what files are going to be there. If you don’t want to include a list of a lot of files in your code, just create a text or XML file with the list in it that you can read at run time.

That’s true, but I kinda wanted to simplify the process. I guess if I must, I’ll use that then. I just didn’t want to countinously update the file list. But oh well.

This seems like the kind of thing you could coax ant into doing automagically whenever you build your jars.

You can always treat the JAR as an Zip file and use the java Zip methods to go through the files in the JAR.

something along the lines of:


ZipInputStream zipin=Thread.currentThread().getContextClassLoader().getResource("<< YOUR JAR FILE >>")..openStream();
ZipEntry zipEntry=zipin.nextEntry();
List fileNames=new ArrayList();
while (zipEntry!=null)
{
  String name=  zipEntry.getName());
  if (name.startsWith("images") fileNames.add(name);
  zipEntry=zipin.nextEntry();
}

moogie, I have quite a few files so it’d be pretty darn slow. But anyway, I used CaptainJester’s idea to use a filelist text file. Works a little better and obviously faster loading.