My files are located under projectRoot/src/main/resources/levels/
If I call Utils.getFileNamesInDirectory("src/main/resources/levels")
, it works.
But when project is packaged, levels
directory is placed under root
of .jar
.
How can I made this dynamic in static class? i.e src/main/resources/
So that code will run within eclipse and as standalone jar.
Code to list files in directory ..
public class Utils {
public static List<String> getFileNamesInDirectory(String directory){
List<String> results = new ArrayList<String>();
File[] files = new File(directory).listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename) {
return filename.endsWith(".json");
}
});
for (File file : files) {
if (file.isFile()) {
results.add(file.getName());
}
}
Collections.sort(results);
return results;
}
}
Updated
I've moved to using getResourceAsStream
(as getResource
was causing IllegalArgumentException: URI is not hierarchical
) and I'm able to list files in a directory within Eclipse
.
public static List<String> getFileNamesInDirectory(String directory){
List<String> results = new ArrayList<String>();
InputStream in = Utils.class.getResourceAsStream("/" + directory);
BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
String line;
try {
while ((line = rdr.readLine()) != null) {
System.out.println("file: " + line);
results.add(new File(line).getName());
}
rdr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
Collections.sort(results);
return results;
}
But when I run it as standalone .jar I get the following error on this line: while ((line = rdr.readLine()) != null) {
Why does it not work outside Eclipse
?
Exception in thread "main" java.lang.NullPointerException
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:322)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:364)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:210)
at java.io.InputStreamReader.read(InputStreamReader.java:205)
at java.io.BufferedReader.fill(BufferedReader.java:165)
at java.io.BufferedReader.readLine(BufferedReader.java:328)
at java.io.BufferedReader.readLine(BufferedReader.java:393)
at com.app.tools.Utils.getFileNamesInDirectory(Utils.java:31)