_JAVA_OPTIONS、JAVA_TOOL_OPTIONS 和 JAVA_OPTS 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28327620/
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
Difference between _JAVA_OPTIONS, JAVA_TOOL_OPTIONS and JAVA_OPTS
提问by Tobber
I thought it would be great to have a comparison between _JAVA_OPTIONS
and JAVA_TOOL_OPTIONS
.
I have been searching a bit for one, but I cannot find anything, so I hope we can find the knowledge here on Stackoverflow.
我认为在_JAVA_OPTIONS
和之间进行比较会很棒JAVA_TOOL_OPTIONS
。我一直在寻找一个,但我找不到任何东西,所以我希望我们可以在 Stackoverflow 上找到知识。
JAVA_OPTS
is included for completeness. It is not part of the JVM, but there is a lot of questions about it out in the wild.
JAVA_OPTS
包括在内是为了完整性。它不是 JVM 的一部分,但在野外有很多关于它的问题。
What I know:
我知道的:
So far I have found out that:
到目前为止,我已经发现:
JAVA_OPTS
is not used by the JDK, but by a bunch of other apps (see this post).JAVA_TOOL_OPTIONS
and_JAVA_OPTIONS
are ways to specify JVM arguments as an environment variable instead of command line parameters.- The are picked up by at least
java
andjavac
- They have this precedence:
_JAVA_OPTIONS
(overwrites the others)- Command line parameters
JAVA_TOOL_OPTIONS
(is overwritten by the others)
- The are picked up by at least
JAVA_OPTS
未被 JDK 使用,而是被许多其他应用程序使用(请参阅此帖子)。JAVA_TOOL_OPTIONS
并且_JAVA_OPTIONS
是将 JVM 参数指定为环境变量而不是命令行参数的方法。- 由至少
java
和javac
- 他们有这样的优先权:
_JAVA_OPTIONS
(覆盖其他)- 命令行参数
JAVA_TOOL_OPTIONS
(被其他人覆盖)
- 由至少
What I would like to know
我想知道什么
- Are there any official documentation comparing
JAVA_TOOL_OPTIONS
and_JAVA_OPTIONS
- Are there any other differences between
JAVA_TOOL_OPTIONS
and_JAVA_OPTIONS
(except from precedence). - Which executables pick up
JAVA_TOOL_OPTIONS
and_JAVA_OPTIONS
(in addition tojava
andjavac
) - Any limitation on what can be included on
JAVA_TOOL_OPTIONS
and_JAVA_OPTIONS
- 是否有任何官方文件比较
JAVA_TOOL_OPTIONS
和_JAVA_OPTIONS
JAVA_TOOL_OPTIONS
和之间是否有任何其他差异_JAVA_OPTIONS
(优先级除外)。- 哪些可执行文件选择
JAVA_TOOL_OPTIONS
和_JAVA_OPTIONS
(除了java
和javac
) - 对可以包含的内容的任何限制
JAVA_TOOL_OPTIONS
和_JAVA_OPTIONS
Official Documentation
官方文档
I have not been able to find any documentation about _JAVA_OPTIONS
. The documentation for JAVA_TOOL_OPTIONS
does not shed much light on the difference:
我无法找到任何有关_JAVA_OPTIONS
. 的文档JAVA_TOOL_OPTIONS
并没有说明差异:
Since the command-line cannot always be accessed or modified, for example in embedded VMs or simply VMs launched deep within scripts, a JAVA_TOOL_OPTIONS variable is provided so that agents may be launched in these cases.
...
由于无法始终访问或修改命令行,例如在嵌入式 VM 或仅在脚本深处启动的 VM 中,提供了一个 JAVA_TOOL_OPTIONS 变量,以便在这些情况下可以启动代理。
...
Example script
示例脚本
This is the code I used to figure this out. Console output is included as comments:
这是我用来解决这个问题的代码。控制台输出作为注释包括在内:
export JAVA_OPTS=foobar
export JAVA_TOOL_OPTIONS=
export _JAVA_OPTIONS="-Xmx512m -Xms64m"
java -version
# Picked up JAVA_TOOL_OPTIONS:
# Picked up _JAVA_OPTIONS: -Xmx512m -Xms64m
# java version "1.7.0_40"
OpenJDK Runtime Environment (IcedTea 2.4.1) (suse-3.41.1-x86_64)
OpenJDK 64-Bit Server VM (build 24.0-b50, mixed mode)
javac -version
# Picked up JAVA_TOOL_OPTIONS:
# Picked up _JAVA_OPTIONS: -Xmx512m -Xms64m
# javac 1.7.0_40
export JAVA_TOOL_OPTIONS="-Xmx1 -Xms1"
export _JAVA_OPTIONS="-Xmx512m -Xms64m"
javac -version
# Picked up JAVA_TOOL_OPTIONS: -Xmx1 -Xms1
# Picked up _JAVA_OPTIONS: -Xmx512m -Xms64m
# javac 1.7.0_40
export JAVA_TOOL_OPTIONS="-Xmx512m -Xms64m"
export _JAVA_OPTIONS="-Xmx1 -Xms1"
javac -version
# Picked up JAVA_TOOL_OPTIONS: -Xmx512m -Xms64m
# Picked up _JAVA_OPTIONS: -Xmx1 -Xms1
# Error occurred during initialization of VM
# Too small initial heap
export JAVA_TOOL_OPTIONS="-Xmx1 -Xms1"
export _JAVA_OPTIONS=
java -Xmx512m -Xms64m -version
# Picked up JAVA_TOOL_OPTIONS: -Xmx1 -Xms1
# Picked up _JAVA_OPTIONS:
# java version "1.7.0_40"
# OpenJDK Runtime Environment (IcedTea 2.4.1) (suse-3.41.1-x86_64)
# OpenJDK 64-Bit Server VM (build 24.0-b50, mixed mode)
export JAVA_TOOL_OPTIONS=
export _JAVA_OPTIONS="-Xmx1 -Xms1"
java -Xmx512m -Xms64m -version
# Picked up JAVA_TOOL_OPTIONS:
# Picked up _JAVA_OPTIONS: -Xmx1 -Xms1
# Error occurred during initialization of VM
# Too small initial heap
采纳答案by mkalkov
You have pretty much nailed it except that these options are picked up even if you start JVM in-process via a library call.
除了即使您通过库调用在进程内启动 JVM 也会选择这些选项之外,您几乎已经掌握了它。
The fact that _JAVA_OPTIONS
is not documented suggests that it is not recommended to use this variable, and I've actually seen people abuse it by setting it in their ~/.bashrc
. However, if you want to get to the bottom of this problem, you can check the source of Oracle HotSpot VM (e.g. in OpenJDK7).
_JAVA_OPTIONS
未记录的事实表明不建议使用此变量,我实际上已经看到人们通过将它设置在他们的~/.bashrc
. 但是,如果您想深入了解这个问题,您可以检查 Oracle HotSpot VM 的来源(例如在 OpenJDK7 中)。
You should also remember that there is no guarantee other VMs have or will continue to have support for undocumented variables.
您还应该记住,不能保证其他 VM 已经或将继续支持未记录的变量。
UPDATE 2015-08-04: To save five minutes for folks coming from search engines, _JAVA_OPTIONS
trumps command-line arguments, which in turn trump JAVA_TOOL_OPTIONS
.
更新 2015-08-04:为了为来自搜索引擎的人节省五分钟,_JAVA_OPTIONS
胜过命令行参数,而命令行参数又胜过JAVA_TOOL_OPTIONS
.
回答by Victor Havin
There is one more difference: _JAVA_OPTIONS
is Oracle specific. IBM JVM is using IBM_JAVA_OPTIONS
instead. This was probably done to be able to define machine-specific options without collisions. JAVA_TOOL_OPTIONS
is recognized by all VMs.
还有一个区别:_JAVA_OPTIONS
是特定于 Oracle 的。IBM JVM 正在使用IBM_JAVA_OPTIONS
。这样做可能是为了能够在没有冲突的情况下定义特定于机器的选项。JAVA_TOOL_OPTIONS
被所有虚拟机识别。
回答by Vadzim
JAVA_OPTS
have no special handlingin JVM at all.
JAVA_OPTS
有没有特殊的处理都在JVM。
And according to https://bugs.openjdk.java.net/browse/JDK-4971166the JAVA_TOOL_OPTIONS
is included in standard JVMTI specification, does better handling of quoted spaces and should be always preferred instead of undocumented Hotspot-specific _JAVA_OPTIONS
.
而根据https://bugs.openjdk.java.net/browse/JDK-4971166将JAVA_TOOL_OPTIONS
被包含在标准规格JVMTI,并更好地处理引用的空间,应始终优先,而不是热点特定无证_JAVA_OPTIONS
。
Also beware that using these prints additional message to stdout that can't be suppressed.
还要注意,使用这些会向 stdout 打印无法被抑制的附加消息。
As @ryenus noted, since JDK 9+, there's JDK_JAVA_OPTIONS as the preferred replacement, see What is the difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?
As @ryenus noted, since JDK 9+, there's JDK_JAVA_OPTIONS as the preferred replacement, see What is the difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?