What is the difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?(使用 Java 11 时 JDK_JAVA_OPTIONS 和 JAVA_TOOL_OPTIONS 有什么区别?)
问题描述
JDK_JAVA_OPTIONS 和 JAVA_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:
这两个变量似乎在启动过程中的不同点实现.
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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Java 11 时 JDK_JAVA_OPTIONS 和 JAVA_TOOL_OPTIONS 有什么区别?
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01