0

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?

  • 1
    It sounds like you want to use this as a resource and include the .json file in your jar file? – matt Mar 26 '21 at 10:14
  • Was just about to point out you should look into resources (instead of files). If you want to access files (for example, in case you want the user to set some configurations in their own target location), it's a good idea to either use an environment variable or input arguments to set the location of the files. – Roy Shahaf Mar 26 '21 at 10:17
  • @matt hmm I have this src/test/resources folder included in Build Path – Janusz Januszewski Mar 26 '21 at 10:18
  • @RoyShahaf those files are part of project – Janusz Januszewski Mar 26 '21 at 10:18
  • Does this answer your question? [How to list the files inside a JAR file?](https://stackoverflow.com/questions/1429172/how-to-list-the-files-inside-a-jar-file) – matt Mar 26 '21 at 10:20
  • You'll need to reference the files as they appear on the classpath. It seems like you're already bundling them as resources. – matt Mar 26 '21 at 10:21
  • @JanuszJanuszewski As I said "you should look into resoruces (instead of files). The latter sentence was basically referring to "when should you use files and how". Clearly in your case you are using a resource so you should look into how that is done (some people have given you links or terms to search for). – Roy Shahaf Mar 27 '21 at 03:03

0 Answers0