File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Using directories inside of a JAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Using directories inside of a JAR file" Watch "Using directories inside of a JAR file" New topic

Using directories inside of a JAR file

Steven Ostrowski

Joined: Jul 31, 2002
Posts: 22
Hello, I have a JAR file that includes some binary files that I use, organized by some directory structure.
For example, inside the directory maps, I have:
Then, inside of those directories, I have the binary files I use.
The code I use to do this is shown here:
String dbName = "texas";

// gets the main maps directory
URL mapsDirURL = getClass().getClassLoader().getResource("com/mystuff/maps");
System.err.println("Trying to load db from path: " + mapsDirURL.getPath());
System.err.println("Trying to load db from file: " + mapsDirURL.getFile());
File mapsDir = new File(mapsDirURL.getPath());

// get the directory of the database being used
File dbDir = new File(mapsDir.getAbsolutePath() + File.separator + dbName);

System.err.println("Database dir is: " + dbDir.getAbsolutePath());

// loop through all files in the directory and construct URLs for each
File[] names = dbDir.listFiles(new ShapeFilenameFilter());

Now, when I have the code laying out on a filesystem ( not in a JAR ), this works fine.
However, when I put it in a JAR and deploy it with WebStart, the dbDir.listFiles command returns null. Unfortunately, it does not throw an exception to say more about what it wants.
With WebStart, I have full security permissions, so there is no security exception.
I think this is a more general problem of how to list "files" from a directory in a JAR like I am doing in the non-JAR case.
I'm needing direction on this ASAP, so if you know, please comment! =)
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I'm thinking it is a problem with multiple ClassLoaders. You are getting the ClassLoader from the current class and asking it to find the file in the jar, but it can't see it.
Try ClassLoader.getSystemClassLoader() or getClass().getSystemResource()
May still not work, but worth a try (if the jar is on the classpath, of course)
I agree. Here's the link:
subject: Using directories inside of a JAR file
It's not a secret anymore!