0

I am making an application where I have to read text-files in order to make my program work. When I run this application in InteliJ it runs perfectly but when I make a .jar file I get a "no such file exception".

  1. Can someone explain me how to access my files inside a .jar file from a class inside the same .jar file?

  2. Can someone also tell me which type of path is more preferable to use (Absolute or relative)?

NOTE: the purpose of the program is to help friends to learn basic words in Arabic. This means that the .jar file will be moved from one computer to the other and it still has to work.

  1. By the way, is putting everything into a .jar something good or are there better ways to put my project in one executable file?

Image of my directory structure

The code i use is:

Path path = Paths.get("src/Multimedia/Woordenschat/Boek1/Arabisch/Hoofdstuk 1.txt");
  • Putting the text file into the jar is fine for your example. Can you show us the directory structure for your jar file? That way will have a better chance of solving your problem. And you definitely want to use a relative path, because you will be moving it from computer to computer. – otoomey Sep 12 '17 at 18:46
  • @Campbell I just added an image of my directory structure. – Endrit Murseli Sep 12 '17 at 19:12
  • I think this will help: https://stackoverflow.com/questions/19035407/classloader-getresourceasstream-returns-null – Christian Kuetbach Sep 12 '17 at 19:49

1 Answers1

0

You can use a BufferedReader and get the file stream with getResourceAsStream():

InputStream in = getClass().getResourceAsStream("path");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
  • I'm not sure but I think the path i use is not correct. Can you try to define the path from my model (red) to my files (blue)? You can find an image of my directory structure above in the question-block. Thank you very much. – Endrit Murseli Sep 12 '17 at 22:39
  • @EndritMurseli if you put the file in the same package as the the class, than you won't need the path, only the filename. – Cardinal System Sep 13 '17 at 10:21
  • Yes it's true I will try it. Thank you very much! – Endrit Murseli Sep 13 '17 at 18:46
  • I have tried to put the file in the same directory and it still doesn't work. **Code:** `Path path = Paths.get("Hoofdstuk 1.txt");` – Endrit Murseli Sep 13 '17 at 20:38
  • No, do not pass a path object. You pass a String object: `getClass().getResourceAsStream("Hoofdstuk 1.txt");` – Cardinal System Sep 13 '17 at 21:32