0

I wanted load .json file with relative path (resources/jsonFiles/json.json). Everything work when I started app from IDE, but when I generate .jar file and run it I had error from nullPointer to file source. I checked this jar file and there path was jsonFiles/json.json (on app work /jsonFiles/json.json). So I changed path and generated new .jar file, but this "mvn package" doesn't work, because junit tests don't work - can't find file in app. I couldn't resolve this problem, thanks for help.

Work on app, but don't work in jar:

URL urlToJson = getClass().getResource("/jsonFiles/json.json");
       exampleList = objectMapper.readValue(new File(urlToJson.getPath())
               , exampleList.class);
damlit
  • 103
  • 1
  • 6
  • and why can't you change the path in your tests? – Pali May 01 '19 at 20:49
  • Good idea, so maybe just path is bad, because I change path in tests (/jsonFiles/json.json), .jar generate well, but when I trying run .jar file is again: java.lang.NullPointerException at example.JsonMapper.(JsonMapper.java:17) at example.Main.main(HomeworkApplication.java:12) So is bad path? I don't know why – damlit May 01 '19 at 20:58
  • https://stackoverflow.com/a/20389418/1879409 – Pali May 01 '19 at 21:07
  • https://stackoverflow.com/a/54225276/1184571 - this is my solution and perfectly solved. getClass() didn't work for my case. – Erçin Akçay May 02 '19 at 01:46

0 Answers0