_JAVA_OPTIONS和JAVA_OPTS之间的差异

时间:2021-09-12 17:19:38

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 *.

我认为在_JAVA_OPTIONS和JAVA_TOOL_OPTIONS之间进行比较会很好。我一直在寻找一个,但是我找不到任何东西,所以我希望我们能在*上找到相关的知识。

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_OPTS不是JDK使用的,而是其他一些应用程序使用的(请参阅本文)。
  • 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 and javac
    • 至少java和javac可以使用
    • They have this precedence:
      1. _JAVA_OPTIONS (overwrites the others)
      2. _JAVA_OPTIONS(覆盖)
      3. Command line parameters
      4. 命令行参数
      5. JAVA_TOOL_OPTIONS (is overwritten by the others)
      6. JAVA_TOOL_OPTIONS(被其他的重写)
    • 它们具有以下优先级:_JAVA_OPTIONS(覆盖其他选项)命令行参数JAVA_TOOL_OPTIONS(被其他选项覆盖)
  • 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
  • 有比较JAVA_TOOL_OPTIONS和_JAVA_OPTIONS的官方文档吗
  • Are there any other differences between JAVA_TOOL_OPTIONS and _JAVA_OPTIONS (except from precedence).
  • 在JAVA_TOOL_OPTIONS和_JAVA_OPTIONS之间还有其他区别吗(除了优先级)。
  • Which executables pick up JAVA_TOOL_OPTIONS and _JAVA_OPTIONS (in addition to java and javac)
  • 哪些可执行程序选择JAVA_TOOL_OPTIONS和_JAVA_OPTIONS(除了java和javac)
  • Any limitation on what can be included on JAVA_TOOL_OPTIONS and _JAVA_OPTIONS
  • 对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

3 个解决方案

#1


28  

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。

#2


16  

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。这样做可能是为了能够在不发生冲突的情况下定义特定于机器的选项。所有vm都可以识别JAVA_TOOL_OPTIONS。

#3


6  

JAVA_OPTS have no special handling in JVM at all.

JAVA_OPTS在JVM中没有任何特殊的处理。

And according to https://bugs.openjdk.java.net/browse/JDK-4971166 the JAVA_TOOL_OPTIONS is included in standard JVMTI specification, does better handling of quoted spaces and should be always preferred to undocumented Hotspot-specific _JAVA_OPTIONS.

根据https://bugs.openjdk.java.net/browse/JDK-4971166, JAVA_TOOL_OPTIONS包含在标准的JVMTI规范中,它可以更好地处理引用的空间,并且应该始终优先于无文档的特定于热点的_JAVA_OPTIONS。

Also beware that using these print additional message to stdout that can't be suppressed.

还要注意,使用这些打印附加消息到不能被抑制的stdout。

#1


28  

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。

#2


16  

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。这样做可能是为了能够在不发生冲突的情况下定义特定于机器的选项。所有vm都可以识别JAVA_TOOL_OPTIONS。

#3


6  

JAVA_OPTS have no special handling in JVM at all.

JAVA_OPTS在JVM中没有任何特殊的处理。

And according to https://bugs.openjdk.java.net/browse/JDK-4971166 the JAVA_TOOL_OPTIONS is included in standard JVMTI specification, does better handling of quoted spaces and should be always preferred to undocumented Hotspot-specific _JAVA_OPTIONS.

根据https://bugs.openjdk.java.net/browse/JDK-4971166, JAVA_TOOL_OPTIONS包含在标准的JVMTI规范中,它可以更好地处理引用的空间,并且应该始终优先于无文档的特定于热点的_JAVA_OPTIONS。

Also beware that using these print additional message to stdout that can't be suppressed.

还要注意,使用这些打印附加消息到不能被抑制的stdout。