0

I want to read a json file in my java application which uses gradle. When I run the application in Intellij and gradle run it works fine, but when I do gradle dist and run that jar it throws a FileNotFound. If I open the jar with winrar I see the file in the correct directory.

I have tried getting the file in all ways I could find, some are:

new FileReader("data/user-data.json");
new FileReader(new File(getClass().getResource("data/user-data.json").getFile()));
new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("data/user-data.json")));

and lots more.

Can anyone help me? Let me know if you need more information.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
cryogenic
  • 55
  • 1
  • 6

1 Answers1

0

You forget to add '/' to the resource path:

getResourceAsStream("/data/user-data.json")

Your jar should look like this:

root (*.jar)
 |-- data
      |-- user-data.json
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35