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.
默认的临时文件目录由系统属性 java.io.tmpdir 指定。在 UNIX 系统上,此属性的默认值通常是“/tmp”或“/var/tmp”;在 Microsoft Windows 系统上,它通常是“c:\temp”。当调用 Java 虚拟机时,可能会为此系统属性赋予不同的值,但对此属性的编程更改不能保证对此方法使用的临时目录产生任何影响。
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.
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.
// 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"; }
// 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"; }
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
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 standardTMPDIR to work, but that is not the case. You can confirm that TMPDIR is not used by running TMPDIR=/mytmp java -XshowSettings
发布评论
评论(10)
根据
java.io.File Java 文档
要指定 java.io.tmpdir 系统属性,您可以按如下方式调用 JVM:
默认情况下,该值应来自 Windows 系统上的 TMP 环境变量
According to the
java.io.File
Java DocsTo specify the
java.io.tmpdir
System property, you can invoke the JVM as follows:By default this value should come from the
TMP
environment variable on Windows systems嗯——因为这是由 JVM 处理的,所以我深入研究了 OpenJDK VM 源代码,认为也许 OpenJDK 所做的事情模仿了 Java 6 及更早版本所做的事情。除了在 Windows 上之外还有其他方法可以做到这一点,这一点并不能令人放心。
在Windows< /a>,OpenJDK 的
get_temp_directory()
函数对GetTempPath()
进行 Win32 API 调用;这就是在 Windows 上 Java 反映TMP
环境变量值的方式。在 Linux< /a> 和 Solaris,相同的
get_temp_directory()
函数返回静态值/tmp/
。我不知道实际的 JDK6 是否遵循这些确切的约定,但从列出的每个平台上的行为来看,它们似乎确实遵循了这些约定。
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 toGetTempPath()
; this is how on Windows, Java reflects the value of theTMP
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_OPTIONS
环境变量。例如,在 bash 中,这可以解决问题:我将其放入我的 bash 登录脚本中,它似乎可以解决问题。
You could set your
_JAVA_OPTIONS
environmental variable. For example in bash this would do the trick:I put that into my bash login script and it seems to do the trick.
使用
Use
它不是环境变量,但仍然可以让您控制临时目录:
例如:
It isn't an environment variable, but still gives you control over the temp dir:
ex.:
要清楚这里发生的情况:
设置临时目录位置的推荐方法是设置名为“java.io.tmpdir”的系统属性,例如通过提供选项
-Djava.io。 tmpdir=/mytempdir
到java
命令。还可以通过调用 System.setProperty("java.io.tmpdir", "/mytempdir) 在程序内更改该属性...模沙箱安全问题。
但是“java.io.tmpdir”系统属性在 JVM 初始化期间仅读取一次,并且其值缓存在
私有
变量中。如果您在此之后以编程方式更改该属性,它将不会对临时目录产生任何影响。如果您没有通过
-D
选项显式设置“java.io.tmpdir”系统属性,JVM 将使用 >特定于平台的默认值。对于 Windows,默认值是通过调用 Win32 API 方法获得的。对于其他 JVM,默认值可能是其他值。根据经验,“TMP”环境变量适用于 Windows(使用当前的 JVM),但不适用于其他平台。如果您关心可移植性,则应该显式设置系统属性。
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 thejava
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.
在 UNIX 终端上使用以下命令:
这将显示所有 java 属性和系统设置。
在此查找
java.io.tmpdir
值。Use below command on UNIX terminal :
This will display all java properties and system settings.
In this look for
java.io.tmpdir
value.如今来源: https://github .com/openjdk/jdk/search?l=Java&p=4&q=java.io.tmpdir 并且仅使用该属性。
对于Linux:
对于 Windows:
https: //github.com/openjdk/jdk/blob/eab4c0c49934bd6f37a0b6174ca10e5c8708d13b/src/hotspot/os/windows/os_windows.cpp#L1366
此处适用于苹果:
https:// github.com/openjdk/jdk/blob/739769c8fc4b496f08a92225a12d07414537b6c0/src/jdk.attach/macosx/native/libattach/VirtualMachineImpl.c#L322
nowadays source: https://github.com/openjdk/jdk/search?l=Java&p=4&q=java.io.tmpdir and only the property is used.
for linux:
for windows:
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
我们可以更改默认的 tomcat 文件上传位置,因为
我们必须设置环境变量,例如:CATALINA_TEMPDIR = 您的文件上传位置。
此位置将更改此处的路径: java -Djava.io.tmpdir=/path/to/tmpdir
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
如果您查看 JDK 的源代码,您可以看到对于 unix 系统,该属性在 从 paths.h 或 硬编码。对于 Windows 来自 win32 返回
tmpdir
名称。对于 posix 系统,您可能期望使用标准
TMPDIR 可以工作,但事实并非如此。您可以通过运行
TMPDIR=/mytmp java -XshowSettings
来确认未使用TMPDIR
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 thetmpdir
name.For posix systems you might expect the standard
TMPDIR
to work, but that is not the case. You can confirm thatTMPDIR
is not used by runningTMPDIR=/mytmp java -XshowSettings