System.getProperty("java.io.tmpdir") not returning correct location

I have read this post and have been sure to set both my system and user variables for TEMP and TMP to C:\Temp and C:\tmp respectively. I have restarted my machine twice, but when my application calls System.getProperty("java.io.tmpdir") it keeps directing to C:\Program Files\Apache Software Foundation\Tomcat 8.5\temp. Why is it doing this and how can I get it to point to C:\Temp or C:\tmp? TIA

EDIT:
I have also tried doing set -Djava.io.tmpdir=C:\Temp from the cmd window and am still getting the same results.

Upvotes: 0

Views: 4508

Answers (2)

Stephen C
Stephen C

Reputation: 719279

"I have also tried doing set -Djava.io.tmpdir=C:\Temp from the cmd window and am still getting the same results."

Tomcat itself is setting the java.io.tmpdir property in the System properties to whatever the CATALINA_TMPDIR environment variable is set to. That happens during Tomcat start-up and it is overriding your -D setting.

So the simple solutions are (as noted in this answer) to either set the CATALINA_TMPDIR in the environment in which Tomcat is launched, or modify the catalina.bat file (or catalina.sh file on Linux). The former would be preferable. It is a good idea to keep the Tomcat installation tree "pristine" if you can.

On the other hand, if you want your webapp to use (say) "C:\Temp" for certain things, while still having the rest of the web container use the java.io.tmpdir for the rest, your webapp code can use (for example)

Files.createTempFile(Path dir, String prefix, String suffix,
                     FileAttribute<?>... attrs) 

instead of

Files.createTempFile(String prefix, String suffix, 
                     FileAttribute<?>... attrs)

i.e. tell it which temp directory to use at the point that you are creating the temp file. (That could be awkward if the temp file is being created in 3rd-party code.)


One final thing to note is that if you are running Tomcat on a SELinux system with enforcement enabled, the Tomcat service is (by default) restricted in where it can read and write files. If you are going to use an alternative "temp" location, you may need to tweak the SELinux policy stuff.

Upvotes: 2

stdunbar
stdunbar

Reputation: 17535

In bin\catalina.bat in the Tomcat installation directory are the lines:

if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
:gotTmpdir

You can either set the environment variable CATALINA_TMPDIR or modify this file.

Upvotes: 8

Related Questions