121

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.

Aaron
  • 896
  • 3
  • 11
  • 22
Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88
  • 4
    http://en.wikipedia.org/wiki/Temporary_folder Looking at the wiki, I'd say you can make it `C:\Temp` by changing %TEMP%, installing Windows 98 or passing it to `java -Djava.io.tmpdir=C:\Temp`. Also check out this: http://stackoverflow.com/questions/3437095/windows-temp-directory-details-java – bezmax May 03 '13 at 05:43
  • @Max Thanks MAX. If I change %TEMP% to `C:\Temp` then I will get `C:\Temp`. Right? but doc show `c:\Temp` instead of `C:\Temp`. :) – Ashish Pancholi May 03 '13 at 05:50
  • 2
    i dont know why the drive letter matters with your application ? – ajduke May 03 '13 at 06:00
  • @AshishPancholi didnt get you there ? – ajduke May 03 '13 at 06:09
  • The 1.4.2 Javadoc you are linking to is outdated. The current [7 Javadoc](http://docs.oracle.com/javase/7/docs/api/java/io/File.html) mentions a "typical" directory of `"C:\\WINNT\\TEMP"`. – Abdull Aug 05 '13 at 14:22
  • To add to @Max's answer, `c:\Temp` was the default in Windows 9x. – krispy Sep 07 '14 at 21:28
  • Java 8 evaluates the environment variable %TMP% in Windows, not %TEMP% – Alex Lehmann Feb 15 '15 at 10:27
  • Notice that it returns a trailing slash. Anyone knows what it returns on unix, and if it also has a trailing slash? – vikingsteve Aug 03 '17 at 12:54
  • For what it's worth, I get C:\Users\AL\AppData\Local\Temp\ – Alonso del Arte Jun 15 '20 at 22:17

4 Answers4

140

In MS Windows the temporary directory is set by the environment variable TEMP. In XP, the temporary directory was set per-user as Local Settings\Temp.

If you change your TEMP environment variable to C:\temp, then you get the same when you run :

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

Madhusudan Joshi
  • 4,438
  • 3
  • 26
  • 42
  • 2
    On Windows there is a second environment variable called %TMP% and it is this which is sometimes used, not %TEMP%, for example the GWT plugin for Eclipse uses the %TMP% variable. – Wee Shetland Oct 10 '13 at 12:15
  • @Joshi : Your answer is quite accurate. However, I disagree with the example you gave : If the user set the `TMP` env var, then the `TEMP` will be ignored. Please, refer to my answer and let me know if you didn't understood what I mean. – Zakaria Apr 18 '15 at 11:43
42

If you set

-Djava.io.tmpdir=C:\temp
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
40

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.

Pod
  • 3,938
  • 2
  • 37
  • 45
Zakaria
  • 14,892
  • 22
  • 84
  • 125
0

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!

Maciek
  • 1,696
  • 12
  • 19