FileNotFoundException when accessing a JSON file in the classpath using java in docker containers (SprintBootApplication)

I am encountering a FileNotFoundException when loading a JSON file that is in the path of a Java Java container using docker containers, this is a Spring-Boot application. This JSON file is available in the resource folder. I can see the JSON file in the docker in the directory. / target / classes /.

Resource resource = resourceLoader.getResource("classpath:folderNm/file.json"); HashMap<String, String> headerMapping = (HashMap<String, String>) parser.parse(new FileReader(resource.getFile().getAbsolutePath())); 

But I get this exception:

 java.io.FileNotFoundException: class path resource [folderNm/file.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/folderNm/file.json 

I tried

-> resource.getFile().getPath(); -> resource.getFile().getCanonicalPath(); -> " ./target/classes/folderName/fileName " (location of the FilePath hard drive) -> " /app.jar!/folderNm/file.json " (location of the FilePath hard drive)

 InputStream inputStream = getClass().getResourceAsStream("xyz.json"); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = br.readLine()) != null) responseStrBuilder.append(inputStr); 

none of the above methods work. Please suggest a way to solve this problem.

+6
source share
2 answers

Assuming your maven structure (you use maven, right?) Your file is in src/main/resources/folderName/file.json , the path you pass to getResourceAsStream should be /folderName/file.json

In fact, everything is explained in javadocs :

Before delegation, the absolute resource name is created from the resource name using this algorithm:

  • If the name begins with '/' ('\ u002f'), then the absolute name of the resource is the part of the name following "/".
  • Otherwise, the absolute name has the following form: modified_package_name / name

If the name modified_package_name is the package name of this object with '/' is replaced by '.' ('\ U002e').

Basically, if you skip the "/" in front, it looks for the folder name inside the package of your class. The following code works fine for me:

  InputStream inputStream = StackTest.class.getResourceAsStream("/folderName/file.json"); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = br.readLine()) != null) responseStrBuilder.append(inputStr); System.out.println(responseStrBuilder.toString()); 

Assuming I have a file.json file in src / main / resources / folderName. I don't think this has anything to do with docker. By the way, I think you could use Apache Commons IOUtils.toString , which helps turn InputStream into a string.

0
source

Create file:

 src/main/resources/firebase/your_account_json.json 

Loading InputStream:

 String path = "/firebase/your_account_json.json"; InputStream in = this.getClass().getResourceAsStream(path); FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(in)) .setDatabaseUrl("https://your-project.firebaseio.com") .setStorageBucket("your-project.appspot.com") .build(); 
0
source

All Articles