I have a java web application by Tomcat 9 servlet container which tries to read a file in /tmp folder with 777 permission on Ubuntu 18.04
ls -ltr /tmp/test.txt
-rwxrwxrwx 1 vagrant vagrant 10 Jan 3 17:03 /tmp/test.txt
The java code is:
try {
result = FileUtils.readFileToString(new File("/tmp/test.txt"));
} catch (IOException ex) {
log.info("##### Cannot read file. Reason: " + ex.getMessage());
}
But it always show the error
##### Cannot read file. Reason: File '/tmp/test.txt' does not exist
This test file can be opened fine by a normal user on a terminal window (example user: vagrant).
vagrant@ras:~$ cat /tmp/test.txt
EDIT: The exact same root issue can cause the slightly different symptom that the Tomcat Manager application cannot be used to deploy a war file located in /tmp. To help future internet searches, here's a typical error you might see in catalina.out when you are encountering this problem. The file /tmp/my_app.war is correctly readable by the user tomcat. Nonetheless you get this problem:
02-Apr-2021 22:25:20.254 SEVERE [http-nio-80-exec-4] org.apache.catalina.startup.ExpandWar.copy Error copying [/tmp/my_app.war] to [/var/lib/tomcat9/webapps/my_app.war]
java.io.FileNotFoundException: /tmp/my_app.war (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.apache.catalina.startup.ExpandWar.copy(ExpandWar.java:276)
at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:1016)
Does anybody know about the problem with Tomcat 9? I'll need the java web application to read this test file as string.