136

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?.

9
  • This is a good question for Oracle/Sun developers. BTW, why is this an issue for you?
    – BackSlash
    Jun 25, 2017 at 10:13
  • 24
    @BackSlash I like to understand they "why" beyond the mere "getting things to work". Jun 25, 2017 at 10:19
  • All great answers; unfortunately I can accept only one. Thank you guys for the enlightenment! Jun 25, 2017 at 10:21
  • 4
    Possible duplicate of In java -D what does the D stand for?
    – Autar
    Jan 21, 2019 at 15:35
  • 1
    @Autar That is not a duplicate. Although it answers the 'Bonus Question', it does not answer the main question. Jan 21, 2019 at 16:04

4 Answers 4

140

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.

But if you use -Dclient, it is now fine as here -Dclient is defined as a system property that is distinct from the -client standard JVM option :

java -jar -Dclient="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.

You have distinct categories of JVM arguments :

1. Standard Options (-D but not only).

These are the most commonly used options that are supported by all implementations of the JVM.

You use -D to specify System properties but most of them don't have any prefix :-verbose, -showversion, and so for...

2. Non-Standard Options (prefixed with -X)

These options are general purpose options that are specific to the Java HotSpot Virtual Machine.
For example : -Xmssize, -Xmxsize

3. Advanced Runtime Options (prefixed with -XX)

These options control the runtime behavior of the Java HotSpot VM.

4. Advanced JIT Compiler Options (prefixed with -XX)

These options control the dynamic just-in-time (JIT) compilation performed by the Java HotSpot VM.

5. Advanced Serviceability Options (prefixed with -XX)

These options provide the ability to gather system information and perform extensive debugging.

6. Advanced Garbage Collection Options (prefixed with -XX)

These options control how garbage collection (GC) is performed by the Java HotSpot VM.


0
50

"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.

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:

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.

2
  • do you have documentation about it?
    – Ori Marko
    Jun 25, 2017 at 10:16
  • 1
    After almost two years I realise, as brilliant as this answer is, it answers the bonus question rather than explaining the need for a prefix in general, so I switched to another answer as the accepted one. Mar 22, 2019 at 14:54
18

If you do not specify anything like -myProp="XYZ" it means it is passed as an argument to main method of the program.

-D means you can use this value using System.getProperty

-X is used for extension arguments like -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.

1
  • 1
    Thanks, the "X" prefix was also on my mind. Do you know if there are any other letters other than "D" and "X" reserved for prefixing special types of arguments? Also, in the above, I see you have "Djava.compiler" under the example for "X" prefixes. Is this correct? Jun 25, 2017 at 11:26
5

Without the -D the properties would conflict with normal JVM options. For example how would you set the property 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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.