aspose file tools*
The moose likes JNLP and Web Start and the fly likes Using a directory 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 » JNLP and Web Start
Bookmark "Using a directory inside of a JAR file" Watch "Using a directory inside of a JAR file" New topic
Author

Using a directory inside of a JAR file

Steven Ostrowski
Greenhorn

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:
maps/texas
maps/florida
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());

Arrays.sort(names);
<<<<<<<<<<<<<<<<<<<<
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! =)
Thanks!
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi,
I think it should work if you make sure that the url contains an exclamation mark after the jar file:
/path/jarfile!/pathinjar
However, I had the same problem some time ago, and I didn't think of trying to use ! in the path. instead, I am using another method from the class loader:
classLoder.getResourceAsStream([path inside jar]);
as your jar is in the class path, so are your binary files. thus, the class loader can load them directly from the classpath. this works fine for me (I am loading gifs this way).
the path you give to the method is the relative path inside the jar. so a gif that is stored at the first level in the jar would have the path "mygif.gif", some file in the directory "resources" inside the jar would have the path "resources/mygif.gif".
this works for the application even if it is not started through JWS, but from an executable jar, or the classpath is accordingly.
hope this was clear
Chantal
Steven Ostrowski
Greenhorn

Joined: Jul 31, 2002
Posts: 22
Originally posted by Chantal Ackermann:
hi,
I think it should work if you make sure that the url contains an exclamation mark after the jar file:
/path/jarfile!/pathinjar
However, I had the same problem some time ago, and I didn't think of trying to use ! in the path. instead, I am using another method from the class loader:
classLoder.getResourceAsStream([path inside jar]);
as your jar is in the class path, so are your binary files. thus, the class loader can load them directly from the classpath. this works fine for me (I am loading gifs this way).
the path you give to the method is the relative path inside the jar. so a gif that is stored at the first level in the jar would have the path "mygif.gif", some file in the directory "resources" inside the jar would have the path "resources/mygif.gif".
this works for the application even if it is not started through JWS, but from an executable jar, or the classpath is accordingly.
hope this was clear
Chantal


Hi Chantal,
Thanks for the reply. I believe I can access the file fine once I know where it is, but what I'm trying to do is list the files in a directory inside the jar, not knowing the names of the files beforehand.
So far, the only thing I've found is instantiating a JarFile with the running jar, and then using the entries() method to get an Enumeration and then filtering the enumeration based on the path to those entries.
Basically, I would like to get the same feature as listFiles(FileNameFilter) that is in the File class.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
I don't think you can list only the files in a specific directory inside a zip compressed file (a jar is a zip file). For what I know every file is compressed with its path as name, so you have a number of files inside the compressed file and from their names the directory structure before the compression is recreated.
to use a method like listFiles() you either need to uncompress the jar/zip or write your own method that filters the files (that's what you have done already).
Chantal
 
Don't get me started about those stupid light bulbs.
 
subject: Using a directory inside of a JAR file