views:

2766

answers:

4

I've used the TMP environment variable to control things like where gcc writes it's temporary files, but I can't seem to find an equivalent for java's createTempFile API.

Does such an environment variable exist?

+2  A: 

It isn't an environment variable, but still gives you control over the temp dir:

-Djava.io.tmpdir

ex.: java -Djava.io.tmpdir=/mytempdir

Bozho
+5  A: 

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.

To specify the java.io.tmpdir System property, you can invoke the JVM as follows:

java -Djava.io.tmpdir=/path/to/tmpdir

By default this value should come from the TMP environment variable on Windows systems

Bryan Kyle
This doesn't answer my question. Is there an environment variable that controls this?
Zach Hirsch
Despite your edit, Bryan, java.io.tmpdir definitely doesn't follow the TMPDIR environment variable on Mac or on Ubuntu (tested on 6.06).
delfuego
Oy, and another edit, making all these comments seem nonsensical.
delfuego
@Zach. The answer is platform specific.
Stephen C
It's not that simple and this answer is still incorrect. On Windows, `java.io.tmpdir` is defined by using the Windows SDK function `GetTempPath` (http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx) which will resolve to TMP or TEMP or USERPROFILE or the Windows directory if each of the previous is not defined.
Pascal Thivent
+6  A: 

Hmmm -- since this is handled by the JVM, I delved into the OpenJDK VM source code a little bit, thinking that maybe what's done by OpenJDK mimics what's done by Java 6 and prior. It isn't reassuring that there's a way to do this other than on Windows.

On Windows (line 996), OpenJDK's get_temp_directory() function makes a Win32 API call to GetTempPath(); this is how on Windows, Java reflects the value of the TMP environment variable.

On Linux (line 1515) and Solaris (line 1828), the same get_temp_directory() functions return a static value of "/tmp/".

I don't know if the actual JDK6 follows these exact conventions, but by the behavior on each of the listed platforms, it seems like they do.

delfuego
Excellent answer, including use of the ever famous "use the source". +1
Paul Wagland
To be clear, what you are looking at is the native code that provides the *default value* for the "java.io.tmpdir" property when the JVM creates the System properties object. This will be overridden by (for example) a "-Djava.io.tmpdir=..." option.
Stephen C
Thanks, delfuego. That certainly does make sense.
Zach Hirsch
@StephenC, yep, that's the point -- the OP was looking for how the default value for the property gets set in the absence of setting it yourself (via the `-Djava.io.tmpdir` command line option to the JVM), and if that default value is affected at all by an environment value. As people had observed, on Windows it **is** affected by the `TMP` environment variable, but it was unclear if there was some unknown variable for other OSes. It looks like there isn't, at least given what we know about OpenJDK.
delfuego
+2  A: 

To be clear about what is going on here:

  • The recommended way to set the temporary directory location is to set the System property called "java.io.tmpdir", e.g. by giving the option -Djava.io.tmpdir=/mytempdir to the java command. The property can also be changed from within a program by calling System.setProperty("java.io.tmpdir", "/mytempdir) ... modulo sandbox security issues.

  • If you don't explicitly set the "java.io.tmpdir" property on startup, the JVM initializes it to a platform specific default value. For Windows, the default is obtained by a call to a Win32 API method. For Linux / Solaris the default is apparently hard-wired. For other JVMs it could be something else.

Empirically, the "TMP" environment variable works on Windows (with current JVMs), but not on other platforms. If you care about portability you should explicitly set the system property.

Stephen C