Unable to read the resource file after deploying to Kubernetes

2/3/2019

I am running local code in traditional tomcat server where as while deploying to Kubernetes my code of read resource file is not working.

I placed nas.txt in resource folder.

and

File file = ResourceUtils.getFile("classpath:nas.txt");

//Read File Content
String content = new String(Files.readAllBytes(file.toPath()));
return content;

This is giving "Internal exception has occurred" error

-- Hearaman
drone
java
kubernetes
spring-boot

2 Answers

11/4/2019

ResourceUtils.getFile doesn't work with packaged jars. Though it works with IDEs. You can try below alternate

protected InputStreamReader readStream(String filePath) throws FileNotFoundException {
    InputStreamReader streamReader;
    if (filePath.startsWith("classpath:")) {
        streamReader = new InputStreamReader(getClass().getResourceAsStream(File.separator + filePath.split("classpath:/*")[1]));
    } else {
        streamReader = new FileReader(ResourceUtils.getFile(filePath));
    }
    return streamReader;
}
-- masT
Source: StackOverflow

2/3/2019

Is the file present in tomcat - - >webapps -->app°war--> resources inside the container

-- P Ekambaram
Source: StackOverflow