3

In my project I have a resources directory with a my_directory inside. my_directory contains text files.

I want to loop this directory with:

URL resource = Resources.class.getResource("/my_directory");
File directory = new File(resource.getPath());
Collection<File> files = FileUtils.listFiles(directory, new String[]{"txt"}, true);

files collection contains all .txt files.

It works well if I run this project in debugger. However if I build project to jar file, it gives an error:

java.lang.IllegalArgumentException: Parameter 'directory' is not a directory

directory file path is:

/home/hsz/.../lib/my_project.jar!/my_directory

How can I use Apache's FileUtils on resources directory ?

hsz
  • 148,279
  • 62
  • 259
  • 315
  • Classpath entries/resources are not necessarily files. You shouldn't be reading them as files. – Sotirios Delimanolis Jun 26 '14 at 15:31
  • @SotiriosDelimanolis So how can I retrieve `/my_directory` content without knowing, what's inside ? – hsz Jun 26 '14 at 15:34
  • This looks like a duplicate question to me, have a read of http://stackoverflow.com/questions/11012819/how-can-i-get-a-resource-folder-from-inside-my-jar-file – Chris K Jun 26 '14 at 15:36

2 Answers2

0

When the files / directories are bundled inside a .jar file, they are no longer be treated as File objects. They can be read by acquiring their inputstream like

InputStream input = getClass().getResourceAsStream("my_directory/file");

I would suggest you to keep the folder with files in the filesystem and make the operations you want to perform. Is there any reason, you want to bundle them into a jar prior to these manipulation.

As per the answer in the below question, In Java 7, you can create a FileSystem from the JAR (zip) file, and then use NIO's directory walking and filtering mechanisms to search through it. This would make it easier to write code that handles JARs and "exploded" directories. You can try that!

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • 1
    I cannot provide files outside of the jar file - it's a plugin file. Is it possible to list files in `my_directory/` as a resource file somehow ? – hsz Jun 26 '14 at 15:41
  • Please check this one - I think you need this http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file – Keerthivasan Jun 26 '14 at 15:44
0
URL url = Resources.class.getResource("/my_directory");
URI uri = url.toURI();
if (uri.getScheme().equals("jar")) {
    // parse JAR file name
    String jarPath = uri.toString().replaceFirst("jar:file:", "").replaceFirst("!.*$", "");
    JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));

    // loop through the entries in the JAR looking for ones in the proper directory
    Enumeration<JarEntry> entries = jar.entries();
    while(entries.hasMoreElements()) {
        String name = entries.nextElement().getName();
        if (name.startsWith(rootDirResource.substring(1))) // or a more complex check for *.txt files
            System.out.println("found: " + name);
    }

    jar.close();
}
fred271828
  • 959
  • 1
  • 11
  • 18