使用 Java 11 时,JDK_JAVA_OPTIONS 和 JAVA_TOOL_OPTIONS 有什么区别?
2022-09-01 07:43:16
使用Java 11时,JDK_JAVA_OPTIONS和JAVA_TOOL_OPTIONS之间的确切区别是什么?
我正在使用一个很小的测试程序:
public class Foo {
public static final void main(String[] args) {
System.out.println("arg: " + System.getProperty("arg"));
}
}
这两个环境变量似乎执行相同的操作,但输出略有不同。这让我相信他们可能有不同的用例:
$ 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
但最终命令行获胜:
$ 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
$ JDK_JAVA_OPTIONS="-Darg=jdk" JAVA_TOOL_OPTIONS="-Darg=tool" javac Foo.java
Picked up JAVA_TOOL_OPTIONS: -Darg=tool
我目前正在使用AdoptOpenJDK 11 build 28。