Java VM 参数类型 -D 和 --

Java VM argument type -D and --

这可能是我问的一个微不足道的问题,但我没有在任何文档中获得此必需信息,可能是我忽略了。目前我使用的是 OpenJDK 8.

编辑:

抱歉,具体用例,我是运行 Spring boot jar。

在传递 JVM 参数时,我们选择

-Dspring.profiles.active=local

但最近我发现 -D 很多地方我发现 --

--spring.profiles.active=local

如果能了解两者的区别以及在何处使用它们,那就太好了。

java 命令的语法是:

java [options] mainclass [args ...]

还有一些替代方案,您可以指定 mainclass 以外的内容,但重要的是 options 出现在该部分之前,而 args 出现在该部分之后,即它们不能混合在一起。

java命令支持很多选项,其中之一是:

-Dproperty=value

Sets a system property value. The property variable is a string with no spaces that represents the name of the property. The value variable is a string that represents the value of the property. If value is a string with spaces, then enclose it in quotation marks (for example -Dfoo="foo bar").

以便它由 Java 运行时处理。

args 是一个程序参数列表,由 Java 代码处理,这意味着它们可以是任何东西。由处理它们的代码来决定它们的含义。

Spring 程序使用 SpringApplication 方法来处理这些参数。查看您的 main 方法:

Register a CommandLinePropertySource to expose command line arguments as Spring properties.

有关详细信息,请参阅 javadoc。

Spring 也有一个 PropertySource 用于使 Java 系统属性可用作 Spring 环境属性,这就是为什么 Spring 可以使用定义的属性这些命令中的:

java -Dfoo=bar mainclass

java mainclass --foo=bar