0

Is it possible to use the new Java 7 NIO file library to traverse a directory tree within a jar in the same way you would on a normal filesystem?

I've got a directory structure I'm reading images from which I'd like to be able to run both in the normal way and also by packaging it as a jar file and running with java -jar.

It seems like the new FileSystem class as well as FileVisitor should be able to achieve this, but there is little documentation on how this can work within a jar. I've tried the following:

private static final FileSystem fileSystem = FileSystems.getDefault();
public static final String workingDir = System.getProperty("user.dir");
Files.walkFileTree(fileSystem.getPath(workingDir, "\res\training\"), new FileVisitor<Path>() {
    ...
    });

This works fine for running from an IDE or by command-line, but when packaged as a jar gives errors similar to:

Exception in thread "main" java.lang.Error:
java.nio.file.NoSuchFileException: C:\Users\rest of path\jar_folder\res\training at util.Main.(Main.java:47) Caused by: java.nio.file.NoSuchFileException: C:\Users\rest of path\jar folder\res\training

Evidently it is attempting to find the files in the directory the jar is running in, rather than inside the jar itself.

Anoop B.K
  • 1,484
  • 2
  • 17
  • 31
user2282497
  • 191
  • 1
  • 9
  • Why? Don't you already know what's in the JAR file? If not, why not? – user207421 Dec 15 '14 at 00:16
  • I know what's in the jar as far as I know the structure is /A/B/... where A is the parent directory, which could have several B directories and ... is the files in those directories. I'm performing image analysis and need to be able to add sets of images easily (i.e. a new B directory with the images within), so don't want to have to hardcode new paths in every time I add more images. – user2282497 Dec 15 '14 at 00:19

0 Answers0