[java] When does System.getProperty("java.io.tmpdir") return "c:\temp"

Just curious as to when System.getProperty("java.io.tmpdir") returns "c:\temp". According to the java.io.File Java Docs-

The default temporary-file directory is specified by the system property java.io.tmpdir. On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp"; on Microsoft Windows systems it is typically "c:\temp". A different value may be given to this system property when the Java virtual machine is invoked, but programmatic changes to this property are not guaranteed to have any effect upon the the temporary directory used by this method.

But in my case-

System.out.println(System.getProperty("java.io.tmpdir"));

Always returns-

C:\Users\admin\AppData\Local\Temp\ i.e. %TEMP%

In what conditions will it return "c:\temp"?

EDITED: If I change %TEMP% to C:\Temp then I will get C:\Temp, right? But the documentation shows c:\Temp instead of C:\Temp.

This question is related to java windows tempdir

The answer is


If you set

-Djava.io.tmpdir=C:\temp

Value of %TEMP% environment variable is often user-specific and Windows sets it up with regard to currently logged in user account. Some user accounts may have no user profile, for example when your process runs as a service on SYSTEM, LOCALSYSTEM or other built-in account, or is invoked by IIS application with AppPool identity with Create user profile option disabled. So even when you do not overwrite %TEMP% variable explicitly, Windows may use c:\temp or even c:\windows\temp folders for, lets say, non-usual user accounts. And what's more important, process might have no access rights to this directory!


On the one hand, when you call System.getProperty("java.io.tmpdir") instruction, Java calls the Win32 API's function GetTempPath. According to the MSDN :

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path specified by the USERPROFILE environment variable.
  4. The Windows directory.

On the other hand, please check the historical reasons on why TMP and TEMP coexist. It's really worth reading.