0

I have a program that works properly in IDE but when I make an executable JAR from it and run it I get errors. Apparently, it is these lines here that cause the error.

File dir = new File("./files");
            File[] directoryListing = dir.listFiles();
            for (File file : directoryListing) {
                array.add(new FileInputStream(file.getPath()));
                System.out.println(file.getPath());
            }

I think that the problem is that the JAR doesn't see/understand my files directory in my project. How can I solve this problem?

Note: I recently switched to IntelliJ and I am not yet comfortable with it

  • the easiest way is to use absolute path instead of relative. For Windows it will be like `C:\Users\user\path\to\files`... Or do some reading about how relative path work in Java [here](https://stackoverflow.com/questions/3844307/how-to-read-file-from-relative-path-in-java-project-java-io-file-cannot-find-th) – Maksym Rudenko Oct 11 '20 at 10:35
  • but I want this JAR to run on other computers too. won't this approach cause problems since the absolute path may differ from one computer to another? @MaksymRudenko – Hrodrsvitnir In Chains Oct 11 '20 at 10:37
  • you can put that file into resource folder and get it with `URL url = getClass().getResource("ListStopWords.txt");` `File file = new File(url.getPath());` – Maksym Rudenko Oct 11 '20 at 10:38
  • I think [this](https://stackoverflow.com/a/40551904/4457451) is the answer to your question – Maksym Rudenko Oct 11 '20 at 10:40
  • Where is that directory when you use your IDE ? Same question when you use your JAR and where is that JAR ? – Amiral ASTERO Oct 11 '20 at 10:42
  • @MaksymRudenko The getPath() method of URL *is not* guaranteed to return a valid path name. Never create a File from that value. The correct way to read a resource is either with URL.openStream, or by using getResourceAsStream instead of getResource. – VGR Oct 11 '20 at 18:14
  • I can reference files that way (using getResource(), getResourceAsStream, etc) but I want to reference a folder because the folder has 289 files and I need to put them in an array by iterating through the folder as seen in my code @MaksymRudenko – Hrodrsvitnir In Chains Oct 11 '20 at 21:08
  • read my last comment please @VGR – Hrodrsvitnir In Chains Oct 11 '20 at 21:09
  • There is no safe way to list resources dynamically. The only reliable way to do it is by including a text file in your .jar, which you can also read with getResource or getResourceAsStream, that contains the resource path of each file. Such a file is fairly easy to generate at build time, since you already know exactly which files you’re putting into a .jar. – VGR Oct 11 '20 at 21:58
  • That makes a lot of sense. I will try it and keep you informed. @VGR – Hrodrsvitnir In Chains Oct 12 '20 at 08:51
  • I am probably doing something wrong with reading a text file from resources because I wrote this line `InputStream text = Main.class.getClass().getClassLoader().getResourceAsStream("src/resources/text.txt");` and got a NullPointerException saying that **Cannot invoke "java.lang.ClassLoader.getResourceAsStream(String)" because the return value of "java.lang.Class.getClassLoader()" is null** @VGR – Hrodrsvitnir In Chains Oct 12 '20 at 17:59
  • You want to load a resource *relative to your own class,* not to java.lang.Class. Try `Main.class.getResourceAsStream("/text.txt");`. – VGR Oct 12 '20 at 18:13
  • IT WORKED! Thank you so much for your help. It says that the question is closed so I don't know if you can write an answer but if you can I will mark it as the answer. @VGR – Hrodrsvitnir In Chains Oct 12 '20 at 18:48

0 Answers0