java options类,使用Java 11时,JDK_JAVA_OPTIONS和JAVA_TOOL_OPTIONS有什么区别?

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

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

$ JDK_JAVA_OPTIONS="-Dstuff" java Foo

NOTE: Picked up JDK_JAVA_OPTIONS: -Dstuff

$ JDK_JAVA_OPTIONS="illegalStuff" java Foo

NOTE: Picked up JDK_JAVA_OPTIONS: illegalStuff

Error: Cannot specify main class in environment variable JDK_JAVA_OPTIONS

$ JAVA_TOOL_OPTIONS="-Dstuff" java Foo

Picked up JAVA_TOOL_OPTIONS: -Dstuff

$ JAVA_TOOL_OPTIONS="illegalStuff" java Foo

Picked up JAVA_TOOL_OPTIONS: illegalStuff

Unrecognized option: illegalStuff

Error: Could not create the Java Virtual Machine.

Error: A fatal exception has occurred. Program will exit.

I'm using a tiny test program:

public class Yo {

public static final void main(String[] args) {

System.out.println(System.getProperty("stuff"));

}

}

It shows that JDK_JAVA_OPTIONS have precedence over JAVA_TOOL_OPTIONS:

$ JDK_JAVA_OPTIONS="-Dstuff=jdk" JAVA_TOOL_OPTIONS="-Dstuff=tool" java Yo

NOTE: Picked up JDK_JAVA_OPTIONS: -Dstuff=jdk

Picked up JAVA_TOOL_OPTIONS: -Dstuff=tool

jdk

But ultimately the command line wins:

$ JDK_JAVA_OPTIONS="-Dstuff=jdk" JAVA_TOOL_OPTIONS="-Dstuff=tool" java -Dstuff=cmd Yo

NOTE: Picked up JDK_JAVA_OPTIONS: -Dstuff=jdk

Picked up JAVA_TOOL_OPTIONS: -Dstuff=tool

cmd

When building, though, only JAVA_TOOL_OPTIONS is read:

$ JDK_JAVA_OPTIONS="-Dstuff=jdk" JAVA_TOOL_OPTIONS="-Dstuff=tool" javac Yo.java

Picked up JAVA_TOOL_OPTIONS: -Dstuff=tool

I'm currently using AdoptOpenJDK 11 build 28.

解决方案

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.

The JDK_JAVA_OPTIONS documentation says:

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.

This line:

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值