Environment variable to control java.io.tmpdir?
Asked Answered
O

10

141

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?

Ocd answered 17/12, 2009 at 19:50 Comment(0)
P
133

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.

Parthen answered 17/12, 2009 at 21:2 Comment(7)
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.Severally
@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.Parthen
Nice answer, but on Windows GetTempPath() is not affected just by the TMP environment variable: msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspxMegalith
I've seen that on Solaris Sun JDK the value is /var/tmp/ (with leading slash), there is even bug#4391434 for that. And for Mac OS and Linux it is /tmp — what a mess!Oxytetracycline
-Djava.io.tmpdir doesn't work on linux with java8 (open jdk 1.8).Mickeymicki
@Mickeymicki does work with oracle-java8-jdk=8u102 (linux)Aetna
On OpenJDK on Ubuntu I get /tmp when writing a small test program that just prints out the property, but for some reason, Google's createTempDir from here, getting that property the same way I did, is coming up with var/tmp, without the leading slash. I determined this by running Druid under strace and looking at all the calls to mkdir.Ramadan
O
150

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

Onaonager answered 17/12, 2009 at 19:54 Comment(4)
This doesn't answer my question. Is there an environment variable that controls this?Ocd
Despite your edit, Bryan, java.io.tmpdir definitely doesn't follow the TMPDIR environment variable on Mac or on Ubuntu (tested on 6.06).Parthen
@Zach. The answer is platform specific.Severally
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 (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.Generous
P
133

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.

Parthen answered 17/12, 2009 at 21:2 Comment(7)
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.Severally
@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.Parthen
Nice answer, but on Windows GetTempPath() is not affected just by the TMP environment variable: msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspxMegalith
I've seen that on Solaris Sun JDK the value is /var/tmp/ (with leading slash), there is even bug#4391434 for that. And for Mac OS and Linux it is /tmp — what a mess!Oxytetracycline
-Djava.io.tmpdir doesn't work on linux with java8 (open jdk 1.8).Mickeymicki
@Mickeymicki does work with oracle-java8-jdk=8u102 (linux)Aetna
On OpenJDK on Ubuntu I get /tmp when writing a small test program that just prints out the property, but for some reason, Google's createTempDir from here, getting that property the same way I did, is coming up with var/tmp, without the leading slash. I determined this by running Druid under strace and looking at all the calls to mkdir.Ramadan
F
77

You could set your _JAVA_OPTIONS environmental variable. For example in bash this would do the trick:

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

I put that into my bash login script and it seems to do the trick.

Figured answered 3/4, 2011 at 16:38 Comment(6)
While this seems to work on Linux and MacOSX, it has the unfortunate side effect of printing some additional output on every java launch: Picked up _JAVA_OPTIONS: This completely confuses our build process.Sheepherder
I did up vote this answer because it does answer the users question and the user didn't specify the OS or wanting to avoid any other side affects. I believe this should be set as best answerBrownley
This variable seems to be specific to Sun JVM -- f.e. the IBM J9 JVM does not support it; it does support IBM_JAVA_OPTIONS insteadSlr
@sendmoreinfo, it works for Sun/Oracle and OpenJDK to my knowledge.Figured
According to this answer about the differences between environment variables, JAVA_TOOL_OPTIONS is more portable.Admittance
Just want to note for anyone who tries this, you may already have that environment variable defined. So may want to keep those previous values. export _JAVA_OPTIONS="${_JAVA_OPTIONS} -Djava.io.tmpdir=/new/tmp"Kinlaw
W
44

Use

$ java -XshowSettings
Property settings:
    java.home = /home/nisar/javadev/javasuncom/jdk1.7.0_17/jre
    java.io.tmpdir = /tmp
Weidar answered 7/8, 2013 at 1:23 Comment(0)
L
31

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

-Djava.io.tmpdir

ex.:

java -Djava.io.tmpdir=/mytempdir
Loop answered 17/12, 2009 at 19:53 Comment(0)
S
23

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 the "java.io.tmpdir" system property is read just once during JVM initialization and its value is cached in private variables. If you programmatically change the property after that point, it will have no effect on the temporary directory.

  • If you don't explicitly set the "java.io.tmpdir" system property via a -D option, the JVM uses 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.

Severally answered 18/12, 2009 at 0:15 Comment(1)
Just a word of caution. Setting this property won't change the directory of things like File.createTempFile will not be affected as it reads the property once to construct a static object that it then uses.Wept
C
12

Use below command on UNIX terminal :

java -XshowSettings

This will display all java properties and system settings. In this look for java.io.tmpdir value.

Cerelly answered 20/6, 2018 at 3:22 Comment(0)
H
1

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"; }

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

Henricks answered 9/4, 2022 at 9:13 Comment(0)
J
0

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

Jansen answered 4/2, 2021 at 4:32 Comment(1)
Please read How to answer and update your answer with guidelines from the guide.Feasible
T
-1

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

Tenedos answered 6/1, 2021 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.