An environment variable to control java.io.tmpdir?

I used the TMP environment variable to manage things like gcc that write temporary files, but I cannot find the equivalent for the java createTempFile API.

Is there such an environment variable?

+79
java environment-variables configuration temp
Dec 17 '09 at 19:50
source share
6 answers

Hmmm - since this is handled by the JVM, I went a little deeper into the OpenJDK VM source code, thinking that it is possible that OpenJDK is done that mimics what has been done in Java 6 and earlier. Not sure if there is a way to do this otherwise than on Windows.

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

On Linux and Solaris , the same get_temp_directory() functions return the static value /tmp/ .

I don’t know if the actual JDK6 conforms to these exact conventions, but the behavior on each of the listed platforms seems to be what they do.

+98
Dec 17 '09 at 21:02
source share

According to java.io.File Java Docs

The default temporary files directory is set by the java.io.tmpdir system property. On UNIX systems, the default value of this property is usually "/ tmp" or "/ var / tmp"; on Microsoft Windows systems, this is usually "c: \ temp". Another value may be assigned to this system property when invoking the Java virtual machine, but programmatic changes to this property do not guarantee any effect on 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 a TMP environment variable on Windows systems

+83
Dec 17 '09 at 19:54
source share

You can set the _JAVA_OPTIONS environment _JAVA_OPTIONS . For example, in bash, this would do the trick:

 export _JAVA_OPTIONS=-Djava.io.tmpdir=/new/tmp/dir 

I put this in my bash login script and it seems to have done the trick.

+45
Apr 03 2018-11-11T00:
source share

Use

 $ java -XshowSettings Property settings: java.home = /home/nisar/javadev/javasuncom/jdk1.7.0_17/jre java.io.tmpdir = /tmp 
+30
Aug 07 '13 at 1:23
source share

This is not an environment variable, but still gives you control over the temporary dir:

 -Djava.io.tmpdir 

eg:.

 java -Djava.io.tmpdir=/mytempdir 
+20
Dec 17 '09 at 19:53
source share

To understand what is going on here:

  • The recommended way to set the temporary location of the directory is to set the System property called "java.io.tmpdir", for example. by setting the -Djava.io.tmpdir=/mytempdir to the java command. The property can also be changed from within the program, causing problems to System.setProperty("java.io.tmpdir", "/mytempdir) ... modulo sandbox.

  • If you do not set the java.io.tmpdir property explicitly at startup, the JVM initializes it to the default value for the platform. For Windows, the default is to call the Win32 API method. For Linux / Solaris, the default is clearly tough. For other JVMs, this 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 must explicitly specify a system property.

+10
Dec 18 '09 at 0:15
source share



All Articles