Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a directory inside of a JAR file

 
Steven Ostrowski
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic