Java 为什么 JVM 参数以“-D”开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44745261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do JVM arguments start with "-D"?
提问by Colm Bhandal
Why do we need to prefix JVM arguments with -D
e.g. when running a jar from the command line? E.g.
-D
当从命令行运行 jar 时,为什么我们需要在 JVM 参数前加上eg前缀?例如
java -jar -DmyProp="Hello World" myProgram.jar
is used to run myProgram.jar
with the system parameter myProp
. So why the leading -D
? Why couldn't the architects of Java let us simply do:
用于myProgram.jar
带系统参数运行myProp
。那么为什么领先-D
呢?为什么 Java 的架构师不能让我们简单地做:
java -jar -myProp="Hello World" myProgram.jar
I'm hoping for an answer beyond just "Because that's the way it is".
我希望得到的答案不仅仅是“因为事情就是这样”。
Bonus Question:Why the letter -D
as opposed to any other letter, does it stand for anything?
额外问题:为什么这个字母-D
与任何其他字母不同,它代表什么?
Note:This question asks whythere was a need to use "D", or any other letter for that matter, in the first place. It is less concerned with the choice of specific letter "D" over any other letter, though that is asked as a bonus question.
注意:这个问题首先询问为什么需要使用“D”或任何其他字母。它不太关心选择特定字母“D”而不是任何其他字母,尽管这是作为奖励问题提出的。
The bonus question has an answer here: In java -D what does the D stand for?.
奖金问题在这里有一个答案:在 java -D 中,D 代表什么?.
采纳答案by davidxxx
Why couldn't the architects of Java let us simply do:
java -jar -myProp="Hello World" myProgram.jar
为什么 Java 的架构师不能让我们简单地做:
java -jar -myProp="Hello World" myProgram.jar
It could work today but suppose that in next Java versions a -myProp
argument is introduced as a JVM option.
How to distinguish your -myProp
from the -myProp
JVM option ? No way.
So it exists an obvious reason to use -D
to definesystem properties.
它今天可以工作,但假设在下一个 Java 版本-myProp
中引入了一个参数作为 JVM 选项。
如何区分你-myProp
的和-myProp
JVM 选项?没门。
所以它存在一个明显的理由-D
来定义系统属性。
As other example, instead of -myProp
suppose you program relies on a -client
system property.
It will not run :
作为另一个例子,而不是-myProp
假设您的程序依赖于-client
系统属性。
它不会运行:
java -jar -client="davidxxx" myProgram.jar
You would have a JVM error such as :
您会遇到 JVM 错误,例如:
Unrecognized option: -client=davidxxx
无法识别的选项:-client=davidxxx
as -client
is a JVM standard option that expects no value.
作为-client
JVM 标准选项,它不期望任何价值。
But if you use -D-client
, it is now fine as here -Dclient
is defined as a system property that is distinct from the -client
standard JVM option :
但是,如果您使用-D-client
,现在就可以了,因为这里-Dclient
定义为不同于-client
标准 JVM 选项的系统属性:
java -jar -D-client="davidxxx" myProgram.jar
Or by using both :
或者同时使用:
java -jar -client -D-client="davidxxx" myProgram.jar
To go further, not all JVM arguments start with -D
.but most of them have a prefix (-D
, -X
, -XX
) that allows in a someway to define namespaces.
更进一步,并非所有 JVM 参数都以-D
. 但它们中的大多数都有一个前缀 ( -D
, -X
, -XX
) 允许以某种方式定义命名空间。
You have distinct categories of JVM arguments :
您有不同类别的 JVM 参数:
1. Standard Options (-D
but not only).
1. 标准选项(-D
但不仅如此)。
These are the most commonly used options that are supported by all implementations of the JVM.
这些是所有 JVM 实现都支持的最常用选项。
You use -D
to specify System properties but most of them don't have any prefix :-verbose
, -showversion
, and so for...
您可以使用-D
指定的系统属性,但大多没有任何前缀-verbose
,-showversion
等了...
2. Non-Standard Options (prefixed with -X
)
2. 非标准选项(以 为前缀-X
)
These options are general purpose options that are specific to the Java HotSpot Virtual Machine.
For example : -Xmssize
, -Xmxsize
这些选项是特定于 Java HotSpot 虚拟机的通用选项。
例如:-Xmssize
,-Xmxsize
3. Advanced Runtime Options (prefixed with -XX
)
3. 高级运行时选项(以 为前缀-XX
)
These options control the runtime behavior of the Java HotSpot VM.
这些选项控制 Java HotSpot VM 的运行时行为。
4. Advanced JIT Compiler Options (prefixed with -XX
)
4. 高级 JIT 编译器选项(以 为前缀-XX
)
These options control the dynamic just-in-time (JIT) compilation performed by the Java HotSpot VM.
这些选项控制由 Java HotSpot VM 执行的动态即时 (JIT) 编译。
5. Advanced Serviceability Options (prefixed with -XX
)
5. 高级可维护性选项(以 为前缀-XX
)
These options provide the ability to gather system information and perform extensive debugging.
这些选项提供了收集系统信息和执行大量调试的能力。
6. Advanced Garbage Collection Options (prefixed with -XX
)
6. 高级垃圾收集选项(以 为前缀-XX
)
These options control how garbage collection (GC) is performed by the Java HotSpot VM.
这些选项控制 Java HotSpot VM 执行垃圾回收 (GC) 的方式。
回答by Deepak Singhal
If you do not specify anything like -myProp="XYZ" it means it is passed as an argument to main method of the program.
如果您没有指定 -myProp="XYZ" 之类的内容,则表示它作为参数传递给程序的 main 方法。
-D means you can use this value using System.getProperty
-D 表示您可以使用 System.getProperty 使用此值
-X is used for extension arguments like -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
-X 用于扩展参数,如 -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
Yes, they could have interchanged.. the characters; but these characters are used to specify what type of parameter is passed and who is the consumer.
是的,他们本可以互换……角色;但是这些字符用于指定传递的参数类型以及谁是消费者。
回答by Henry
Without the -D
the properties would conflict with normal JVM options. For example how would you set the property jar
?
没有这些-D
属性会与普通的 JVM 选项冲突。例如,您将如何设置属性jar
?
The -D
was probably chosen (I can only speculate about that) because it is also used in the C preprocessor to define symbols and was therefore familiar to most people.
将-D
很可能选择(我只能推测有关),因为它是在C预处理器还用于定义符号,因此是最熟悉的人。
回答by Rakurai
"Define". The meaning is similar to a preprocessor definition in C. The -D signifies that the definition is in the context of the application, and not in the Java interpreter context like any other option before the executable name.
“定义”。其含义类似于 C 中的预处理器定义。 -D 表示该定义在应用程序的上下文中,而不是像可执行文件名称之前的任何其他选项一样在 Java 解释器上下文中。
The usage of the letter "D" isn't specifically explained in the documentation, but the only use is to "define" a key in the system properties map - except for this reference:
文档中没有特别解释字母“D”的用法,但唯一的用途是在系统属性映射中“定义”一个键 - 除了这个参考:
The System class maintains a Properties object that defines the configuration of the current working environment. For more about these properties, see System Properties. The remainder of this section explains how to use properties to manage application configuration.
System 类维护一个 Properties 对象,该对象定义当前工作环境的配置。有关这些属性的更多信息,请参阅系统属性。本节的其余部分说明如何使用属性来管理应用程序配置。