How can I solve this situation? I have a static JSON file on my src/main/resources
and when I run the code, I use this JSON to create some data, but when I try to deploy on heroku
I get this error, probably because they couldn’t find the FUNDOS.json
file.
Does anyone have an idea on how to solve that?
Code
String FUNDOS_JSON = "src/main/resources/data/FUNDOS.json"; Path filePath = Path.of(FUNDOS_JSON).toAbsolutePath(); String jsonString = Files.readString(filePath); JSONArray json = new JSONArray(jsonString);
Error
remote: [ERROR] COMPILATION ERROR : remote: [INFO] ------------------------------------------------------------- remote: [ERROR] /tmp/build_835ef365/src/main/java/br/anbima/mvc/service/AnbimaServiceImpl.java:[36,37] cannot find symbol remote: symbol: method of(java.lang.String) remote: location: interface java.nio.file.Path remote: [ERROR] /tmp/build_835ef365/src/main/java/br/anbima/mvc/service/AnbimaServiceImpl.java:[37,42] cannot find symbol remote: symbol: method readString(java.nio.file.Path) remote: location: class java.nio.file.Files remote: [INFO] 2 errors
Advertisement
Answer
For assets inside src/main/resources use getResourceAsStream or getResource.
Suppose your code is in a file named Foo.java,
InputStream fundosStream = Foo.class.getResourceAsStream("/data/FUNDOS.json"); String fundosContent = ""; try(BufferedReader br = new BufferedReader(new InputStreamReader(fundosStream)) { String line = br.readLine(); while(line != null) { fundosContent = line + "n"; line = br.readLine(); } } catch(IOException ioe) { ioe.printStackTrace(); } JSONArray jsonArray = new JSONArray(fundosContent);