为什么JVM参数以“ -D”开头? [英] Why do JVM arguments start with "-D"?
问题描述
为什么我们需要在JVM参数前面加上 -D
作为前缀,例如从命令行运行jar时?例如,
Why do we need to prefix JVM arguments with -D
e.g. when running a jar from the command line? E.g.
java -jar -DmyProp="Hello World" myProgram.jar
用于运行 myProgram.jar
和系统参数 myProp
。那么,为什么前导 -D
?为什么Java的架构师不能让我们简单地做:
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:
java -jar -myProp="Hello World" myProgram.jar
我希望得到的答案不仅仅是因为那样。
I'm hoping for an answer beyond just "Because that's the way it is".
奖金问题:为什么字母 -D
与其他字母相反,
Bonus Question: Why the letter -D
as opposed to any other letter, does it stand for anything?
注意:这个问题问为什么首先,需要使用 D或其他任何字母。相对于其他任何字母,它都不需要选择特定字母 D,尽管这是作为奖金问题提出的。
Note: This question asks why there 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.
奖金问题在这里有答案: 在Java -D中D代表什么?。
The bonus question has an answer here: In java -D what does the D stand for?.
推荐答案
为什么Java的架构师不能让我们简单地做:
Why couldn't the architects of Java let us simply do:
java -jar -myProp = Hello World myProgram.jar
它现在可以工作,但假设在下一个Java版本中,引入 -myProp
参数作为JVM选项。
如何区分 -myProp
和 -myProp
JVM选项?
因此,使用 -D
来定义系统属性是一个明显的理由。
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 define system properties.
作为另一个示例,假设您的程序依赖于 -client,而不是
系统属性。 -myProp
它不会运行:
As other example, instead of -myProp
suppose you program relies on a -client
system property.
It will not run :
java -jar -client="davidxxx" myProgram.jar
您将遇到JVM错误,例如:
You would have a JVM error such as :
无法识别的选项:-client = davidxxx
Unrecognized option: -client=davidxxx
为 -client
是JVM标准选项,期望没有值。
as -client
is a JVM standard option that expects no value.
但是,如果您使用 -D客户端
,现在可以使用- Dclient
定义为不同于 -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 :
java -jar -D-client="davidxxx" myProgram.jar
或同时使用两者:
java -jar -client -D-client="davidxxx" myProgram.jar
要走得更远,并不是所有的JVM参数都以 -D $开头c $ c>。
,但大多数都带有前缀( -D
, -X
, -XX
)允许以某种方式定义名称空间。
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参数:
You have distinct categories of JVM arguments :
1。标准选项( -D
,但不仅如此)。
1. Standard Options (-D
but not only).
这些是最常用的选项, JVM的所有实现都支持。
These are the most commonly used options that are supported by all implementations of the JVM.
您使用 -D
来指定系统属性,但其中大多数没有任何前缀: -verbose
, -showversion
,等等……
You use -D
to specify System properties but most of them don't have any prefix :-verbose
, -showversion
, and so for...
2。非标准选项(以 -X
为前缀)
2. Non-Standard Options (prefixed with -X
)
这些选项是通用选项,它们是特定于Java HotSpot虚拟机。
例如: -Xmssize
, -Xmxsize
These options are general purpose options that are specific to the Java HotSpot Virtual Machine.
For example : -Xmssize
, -Xmxsize
3。高级运行时选项(以 -XX
为前缀)
3. Advanced Runtime Options (prefixed with -XX
)
这些选项控制Java的运行时行为热点VM。
These options control the runtime behavior of the Java HotSpot VM.
4。高级JIT编译器选项(前缀为 -XX
)
4. Advanced JIT Compiler Options (prefixed with -XX
)
这些选项控制动态准入Java HotSpot VM执行实时(JIT)编译。
These options control the dynamic just-in-time (JIT) compilation performed by the Java HotSpot VM.
5。先进的可维护性选项(以 -XX
为前缀)
5. Advanced Serviceability Options (prefixed with -XX
)
这些选项提供了收集系统信息的能力并执行广泛的调试。
These options provide the ability to gather system information and perform extensive debugging.
6。高级垃圾收集选项(前缀为 -XX
)
6. Advanced Garbage Collection Options (prefixed with -XX
)
这些选项控制垃圾收集的方式(GC )由Java HotSpot VM执行。
These options control how garbage collection (GC) is performed by the Java HotSpot VM.
这篇关于为什么JVM参数以“ -D”开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!