Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using directories inside of a JAR file

Steven Ostrowski
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:
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic