Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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?
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
–
–
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, 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 and Solaris, 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.
–
–
–
–
–
–
–
–
$ java -XshowSettings
Property settings:
java.home = /home/nisar/javadev/javasuncom/jdk1.7.0_17/jre
java.io.tmpdir = /tmp
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. However, note that the property is not consulted dynamically. It is read once during class initialization. If you programmatically change the property after that point, it has not effect on the temporary directory.
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.
–
we can change the default tomcat file upload location, as
we have to set the environment variable like : CATALINA_TEMPDIR = YOUR FILE UPLOAD LOCATION.
this location will change the path here: java -Djava.io.tmpdir=/path/to/tmpdir
nowadays source: https://github.com/openjdk/jdk/search?l=Java&p=4&q=java.io.tmpdir and only the property is used.
for linux:
// This must be hard coded because it's the system's temporary
// directory not the java application's temp directory, ala java.io.tmpdir.
const char* os::get_temp_directory() { return "/tmp"; }
https://github.com/openjdk/jdk/blob/eab4c0c49934bd6f37a0b6174ca10e5c8708d13b/src/hotspot/os/linux/os_linux.cpp#L1317
https://github.com/openjdk/jdk/blob/739769c8fc4b496f08a92225a12d07414537b6c0/src/java.base/unix/conf/net.properties
for windows:
if (GetTempPath(MAX_PATH, path_buf) > 0) {
return path_buf;
} else {
https://github.com/openjdk/jdk/blob/eab4c0c49934bd6f37a0b6174ca10e5c8708d13b/src/hotspot/os/windows/os_windows.cpp#L1366
and here for apple:
https://github.com/openjdk/jdk/blob/739769c8fc4b496f08a92225a12d07414537b6c0/src/jdk.attach/macosx/native/libattach/VirtualMachineImpl.c#L322
If you look in the source code of the JDK, you can see that for unix systems the property is read at compile time from the paths.h or hard coded. For windows the function GetTempPathW
from win32 returns the tmpdir
name.
For posix systems you might expect the standard TMPDIR
to work, but that is not the case. You can confirm that TMPDIR
is not used by running TMPDIR=/mytmp java -XshowSettings
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.