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
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".
Bonus Question: Why the letter -D
as opposed to any other letter, does it stand for anything?
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.
The bonus question has an answer here: In java -D what does the D stand for?.
ベストアンサー1
Why couldn't the architects of Java let us simply do:
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 define system properties.
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
You would have a JVM error such as :
Unrecognized option: -client=davidxxx
as -client
is a JVM standard option that expects no value.
ただし、 を使用する場合は、標準の JVM オプションとは異なるシステム プロパティとして定義されている-Dclient
ため、問題はありません。-Dclient
-client
java -jar -Dclient="davidxxx" myProgram.jar
または両方を使用することもできます:
java -jar -client -D-client="davidxxx" myProgram.jar
さらに、すべての JVM 引数が で始まるわけではありません-D
。しかし、それらのほとんどには、何らかの方法で名前空間を定義できるようにするプレフィックス ( -D
、、 ) があります。-X
-XX
JVM 引数には異なるカテゴリがあります。
1. 標準オプション(-D
ただしそれだけではありません)。
これらは、JVM のすべての実装でサポートされている最も一般的に使用されるオプションです。
システム プロパティを指定するために使用します-D
が、そのほとんどにはプレフィックス : 、 などが付いていませ-verbose
ん-showversion
。
2. 非標準オプション( が付く-X
)
これらのオプションは、Java HotSpot 仮想マシンに固有の汎用オプションです。
例-Xmssize
:-Xmxsize
3. 高度なランタイムオプション( で始まる-XX
)
これらのオプションは、Java HotSpot VM の実行時の動作を制御します。
4. 高度な JIT コンパイラ オプション (接頭辞-XX
)
これらのオプションは、Java HotSpot VM によって実行される動的ジャストインタイム (JIT) コンパイルを制御します。
5. 高度な保守オプション( が付く-XX
)
これらのオプションを使用すると、システム情報を収集し、広範なデバッグを実行することができます。
6. 高度なガベージコレクションオプション( で始まる-XX
)
これらのオプションは、Java HotSpot VM によるガベージ コレクション (GC) の実行方法を制御します。