• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Using a directory inside of a JAR file

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic