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
  • 90,369
  • 161
  • 124
  • 175
cryogenic
  • 45
  • 1
  • 5

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
  • 15,340
  • 4
  • 20
  • 31