1

I have the problem of wanting to list all files in a directory. The directory in the IDE is src/main/resources/commands/cirno/, in the jar, it's commands/cirno/:

/
    commands/
        cirno/
            file1
            file2

What I tried is opening an InputStream and listing the lines with a BufferedReader:

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("commands/cirno/");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    ArrayList<String> strings = new ArrayList<>();
    br.lines().forEach(strings::add);
    strings.forEach(System.out::println);

This works in the IDE, the console output is as follows:

file1
file2

The problem I is that it stops working when packaged as .jar. It still works when I open a single file, but when I try to open a folder, the returned InputStream is empty. It is not null, as is == null returns false, but br.readLine() returns an empty String.

Hontaro
  • 83
  • 6
  • This question is specific about searching resources inside jar file. The linked question provides solution only to unpacked classpath. – Ivan Balashov Mar 30 '17 at 14:14

0 Answers0