2

I have a project that uses this code to get a filtered list of files in a directory:

Path directoryPath = 
    FileSystems.getDefault().getPath("src/main/resources/com/foo/bar");
DirectoryStream<Path> ds = 
    Files.newDirectoryStream(directoryPath, filter);

This works great when I run the project in eclipse, but when we package up the jar, of course, it can't find the resources using this path. I could manually reference these resources if I knew their names ahead of time, but I'm hitting a wall trying to easily filter them, or even get a list of them. Is there a clean way to do this that references these files as resources instead of a specific directory location?

Alex Pritchard
  • 4,260
  • 5
  • 33
  • 48
  • `"src/main/resources/com/foo/bar"` is already a problem. Your `src` directory won't be there at runtime. You should be using `Class.getResource()` and friends, not files and paths. – user207421 Dec 01 '13 at 21:23
  • I would normally reference all of these as resources, but I have thus far been able to find any way to filter resources at runtime, the way Files.newDirectoryStream(path, globFilter) does. For example, if you want to load all the resources in a directory specified by command-line arg. – Alex Pritchard Dec 02 '13 at 16:27

1 Answers1

2

Have a look at Class.getResource or Class.getResourceAsStream and see if that works for you.

Marc
  • 78
  • 9
  • 2
    It seems I didn't read your question thoroughly enough :-S have a look at this http://stackoverflow.com/questions/5193786/how-to-use-classloader-getresources-correctly or the second answer to this if you're willing to go to this extent http://stackoverflow.com/questions/11012819/how-can-i-get-a-resource-folder-from-inside-my-jar-file – Marc Nov 27 '13 at 21:04
  • Thanks, @user1181247. I was able to use the information in the first link you posted to come up with a solution. – Alex Pritchard Nov 29 '13 at 20:06