控制java.io.tmpdir的环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1924136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:29:05  来源:igfitidea点击:

Environment variable to control java.io.tmpdir?

javaconfigurationenvironment-variablestemp

提问by Zach Hirsch

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

我已经使用TMP环境变量来控制诸如 gcc 在何处写入它的临时文件之类的事情,但我似乎找不到 java 的createTempFileAPI的等效项。

Does such an environment variable exist?

是否存在这样的环境变量?

采纳答案by delfuego

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.

嗯——因为这是由 JVM 处理的,所以我深入研究了 OpenJDK VM 源代码,认为 OpenJDK 所做的可能模仿了 Java 6 和更早版本所做的。除了在 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 TMPenvironment variable.

Windows 上,OpenJDK 的get_temp_directory()函数调用 Win32 API GetTempPath();这就是在 Windows 上,Java 反映TMP环境变量值的方式。

On Linuxand Solaris, the same get_temp_directory()functions return a static value of /tmp/.

LinuxSolaris 上,相同的get_temp_directory()函数返回静态值/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.

我不知道实际的 JDK6 是否遵循这些确切的约定,但是从每个列出的平台上的行为来看,它们似乎确实如此。

回答by Bozho

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

它不是环境变量,但仍可让您控制临时目录:

-Djava.io.tmpdir

ex.:

前任。:

java -Djava.io.tmpdir=/mytempdir

回答by Bryan Kyle

According to the java.io.FileJava Docs

根据java.io.FileJava 文档

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.

默认的临时文件目录由系统属性 java.io.tmpdir 指定。在 UNIX 系统上,此属性的默认值通常是“/tmp”或“/var/tmp”;在 Microsoft Windows 系统上,它通常是“c:\temp”。调用 Java 虚拟机时,可能会为此系统属性赋予不同的值,但不能保证对此属性的编程更改对此方法使用的临时目录有任何影响。

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

要指定java.io.tmpdirSystem 属性,您可以按如下方式调用 JVM:

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

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

默认情况下,此值应来自TMPWindows 系统上的环境变量

回答by Stephen C

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=/mytempdirto the javacommand. 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 specificdefault 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.

  • 设置临时目录位置的推荐方法是通过给选项中设置所谓的“java.io.tmpdir”系统属性,例如-Djava.io.tmpdir=/mytempdirjava命令。也可以通过调用System.setProperty("java.io.tmpdir", "/mytempdir)... modulo sandbox security issues从程序内部更改该属性。

  • 如果您未在启动时显式设置“java.io.tmpdir”属性,JVM 会将其初始化为特定平台的默认值。对于 Windows,默认值是通过调用 Win32 API 方法获得的。对于 Linux / Solaris,默认显然是硬连线的。对于其他 JVM,它可能是别的东西。

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.

根据经验,“TMP”环境变量适用于 Windows(使用当前的 JVM),但不适用于其他平台。如果您关心可移植性,您应该明确设置系统属性。

回答by John St. John

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

你可以设置你的_JAVA_OPTIONS环境变量。例如在 bash 中,这可以解决问题:

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

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

我把它放到我的 bash 登录脚本中,它似乎可以解决问题。

回答by Nisar Ahmed

Use

$ java -XshowSettings
Property settings:
    java.home = /home/nisar/javadev/javasuncom/jdk1.7.0_17/jre
    java.io.tmpdir = /tmp

回答by suhas

Use below command on UNIX terminal :

在 UNIX 终端上使用以下命令:

java -XshowSettings

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

这将显示所有 java 属性和系统设置。在此寻找java.io.tmpdir价值。