使用 Java 11 时 JDK_JAVA_OPTIONS 和 JAVA_TOOL_OPTIONS 有什么区别? [英] What is the difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?

查看:30
本文介绍了使用 Java 11 时 JDK_JAVA_OPTIONS 和 JAVA_TOOL_OPTIONS 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JDK_JAVA_OPTIONSJAVA_TOOL_OPTIONS 使用 Java 11 时?

What is the exact difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?

我正在使用一个小型测试程序:

I'm using a tiny test program:

public class Foo {
  public static final void main(String[] args) {
    System.out.println("arg: " + System.getProperty("arg"));
  }
}

这两个环境变量似乎做的一样,但输出略有不同.这让我相信他们可能有不同的用例:

The two environment variables seem to do the same, but the output is slightly different. That makes me believe they might have different use cases:

$ JDK_JAVA_OPTIONS="-Darg=jdk" java Foo
NOTE: Picked up JDK_JAVA_OPTIONS: -Darg
arg: jdk

$ JAVA_TOOL_OPTIONS="-Darg=tool" java Foo
Picked up JAVA_TOOL_OPTIONS: -Darg
arg: tool

$ JDK_JAVA_OPTIONS="illegalArg" java Foo
NOTE: Picked up JDK_JAVA_OPTIONS: illegalArg
Error: Cannot specify main class in environment variable JDK_JAVA_OPTIONS

$ JAVA_TOOL_OPTIONS="illegalArg" java Foo
Picked up JAVA_TOOL_OPTIONS: illegalArg
Unrecognized option: illegalArg
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

似乎 JDK_JAVA_OPTIONS 优先于 JAVA_TOOL_OPTIONS:

$ JDK_JAVA_OPTIONS="-Darg=jdk" JAVA_TOOL_OPTIONS="-Darg=tool" java Foo
NOTE: Picked up JDK_JAVA_OPTIONS: -Darg=jdk
Picked up JAVA_TOOL_OPTIONS: -Darg=tool
arg: jdk

但最终还是命令行获胜:

But ultimately the command line wins:

$ JDK_JAVA_OPTIONS="-Darg=jdk" JAVA_TOOL_OPTIONS="-Darg=tool" java -Darg=cmd Foo
NOTE: Picked up JDK_JAVA_OPTIONS: -Darg=jdk
Picked up JAVA_TOOL_OPTIONS: -Darg=tool
arg: cmd

但是,在构建时,只读取 JAVA_TOOL_OPTIONS:

When building, though, only JAVA_TOOL_OPTIONS is read:

$ JDK_JAVA_OPTIONS="-Darg=jdk" JAVA_TOOL_OPTIONS="-Darg=tool" javac Foo.java
Picked up JAVA_TOOL_OPTIONS: -Darg=tool

我目前正在使用 AdoptOpenJDK 11 build 28.

I'm currently using AdoptOpenJDK 11 build 28.

推荐答案

@gjoranv 的回答解释了两个变量之间的功能差异.

The functional difference between the two variables is explained by @gjoranv's answer.

我认为输出的差异源于以下几点:

The differences in the output I think stem from the following:

  1. 这两个变量似乎在启动过程中的不同点实现.

  1. The two variables seem to be implemented in different points in the launching process.

JDK_JAVA_OPTIONS 文档说:

为了减少对 JDK_JAVA_OPTIONS 行为的潜在滥用,环境变量中不允许指定主类(例如 -jar)或导致 java 启动器退出而不执行主类的选项(例如 -h).如果这些选项中的任何一个出现在环境变量中,启动器将中止并显示错误消息.

In order to mitigate potential misuse of JDK_JAVA_OPTIONS behavior, options that specify the main class (such as -jar) or cause the java launcher to exit without executing the main class (such as -h) are disallowed in the environment variable. If any of these options appear in the environment variable, the launcher will abort with an error message.

这一行:

 Error: Cannot specify main class in environment variable JDK_JAVA_OPTIONS

是警告用户可能尝试通过该变量进行破坏的错误消息.

is the error message that warns the user of a potential attempt to do mayhem via that variable.

我认为 JDK_JAVA_OPTIONS 优先,部分原因是相同的.

I think that JDK_JAVA_OPTIONS takes precedence, in part for the same reason.

这篇关于使用 Java 11 时 JDK_JAVA_OPTIONS 和 JAVA_TOOL_OPTIONS 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆