I have json files in directory: C:\Users\pim\Documents\triage\src\test\resources\jsons
and I have method which returns File[]:
public static File[] listFiles() throws IOException {
File dir = new File("src/test/resources/jsons");
if (dir.isFile()) {
throw new IllegalArgumentException(dirString);
}
Path folderPath = dir.toPath().toAbsolutePath();
List<File> files = new LinkedList<File>();
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(folderPath)) {
for (Path path : directoryStream) {
if (path.toString().endsWith(".json")) {
File file = new File(path.toString());
if (file != null) {
files.add(file);
}
}
}
} catch (IOException ex) {
System.err.println("Error reading files");
ex.printStackTrace();
}
return files.stream().toArray(File[]::new);
}
It runs correctly on Eclipse, but when I run it from bash as jar it returns:
NoSuchFileException: C:\Users\pim\Documents\triage\target\src\test\resources\jsons
Why it looks in this path:
C:\Users\pim\Documents\triage\target\src\test\resources\jsons
instead of this:
C:\Users\pim\Documents\triage\src\test\resources\jsons
How to change it? It looks like the path changes with jar location.
Those jsons are located in target folder after build:
C:\Users\pim\Documents\triage\target\test-classes\jsons
How to point this jar to look for this in his relative path only when application is running by jar?