-1

I am trying to build my project to a jar file. When I first opened the jar file I noticed nothing happened. I then launched the jar file from cmd. Then I saw it throws an error because it cannot find a txt file. This is the error:

enter image description here

Executing the program in IntelliJ works fine so I am assuming the txt file is not included in the artifact. This is how I use the txt file in the code:

enter image description here

This is what my file structure looks like:

enter image description here

This is what my artifact settings look like:

enter image description here

I tried putting the txt file in the src folder and manually including it in the artifact settings, I tried putting the txt file in the resources folder (It is marked as Resources Root) and I tried using the resources folder and also manually adding the resources folder to the artifact settings (this is the last thing I tried and is visible in the picture of my artifact settings).

I am really confused why the txt file doesn't seem to be in the jar file. Any help would be really appreciated.

ocrdu
  • 2,172
  • 6
  • 15
  • 22

2 Answers2

0

Place the file in the resources folder, and instead of File try using getResourceAsStream to get the contents of the file:

InputStream in = getClass().getResourceAsStream("/data.txt");
Scanner pathScanner = new Scanner(in);
Amita
  • 944
  • 1
  • 8
  • 20
  • Thank you this works! However I forgot to mention that in another part of the code I also use the data.txt file but here I don't read it but write to it like this: File pathFile = new File("resources/data.txt"); FileWriter pathWriter = new FileWriter(pathFile); How can I make this work with the InputStream? – Borek Bandell Jan 18 '21 at 17:12
  • So you asked the wrong question. It should be "I can't read a txt file from a jar" instead of "The file is not included.". You know a jar file is just a zip file that you can open and check? – SurfMan Jan 18 '21 at 21:08
  • @SurfMan I asked the wrong question because I had no idea what was going on. I know a bit of basic java and tried to compile it to a jar file. In the meantime I found out you can't change jar files so you will have to store data somewhere else. – Borek Bandell Jan 18 '21 at 23:27
  • @BorekBandell you can try injecting a file into the jar like this: `.jar -C resources data.txt`. Not sure if this can fit your use-case, but it is used in cases, say you have a config file on the server, and you need your jar to use those values from there – Amita Jan 19 '21 at 13:12
  • For your question, to write to a file you would need not this `InputStream` but an `OutputStream`. Also, make sure you close these streams once finished. @BorekBandell – Amita Jan 19 '21 at 13:15
0

Make sure to set ?*.txt as resource pattern in the compiler options.

enter image description here

SurfMan
  • 1,685
  • 12
  • 19
  • For me it did not contain .txt so I added it. Then I tried running the jar file but now I get a no main manifest attribute error even though I did select a main class. Edit: nevermind I tried creating the artifact again and now this error is gone. I still see the original error. – Borek Bandell Jan 18 '21 at 17:01