Sonia Jain
Sonia Jain

Reputation: 153

What is -D in vm arguments what it indicates why we have to specify always -D in vm arguments

What is -D in vm arguments what it indicates why we have to specify always -D in vm arguments. Is there any standard? if yes what is that and where it is specified.

Upvotes: 3

Views: 2181

Answers (2)

Stephen C
Stephen C

Reputation: 719238

The -D option is for passing system properties to the JVM. The name / value pairs are added to the System properties object, and may be used during JVM initialization.

why we have to specify always -D in vm arguments

(If you are asking why all command line options in the java command that could be construed as "VM arguments" start with -D ... you are mistaken. Many of them don't start with -D. Check the java command manual page.)

If you set the properties in (your) code using System.setProperty(...), the JVM may have already looked up the property before your code runs. A lot of the JVM's initialization is triggered by class loading / static initialization. It is often difficult to get your code to run before the relevant static initialization.

On the other hand, a typical application doesn't need to have system properties set. The default settings are typically good enough.

Is there any standard? if yes what is that and where it is specified.

There are a number of system properties that the JVM recognizes. They are documented in the javadocs and other places, but there is no single page that covers them all (AFAIK).

But your code and 3rd-party libraries can also define their own system properties.

References:

The above list is not exhaustive ...

Upvotes: 1

locus2k
locus2k

Reputation: 2935

The -D sets property values the java program currently running has access to. It allows the programmer to set values that are needed for the program to run but the program doesn't know what those values are so the user has to set them. This can be as simple as setting a file path or a resource location to more complex things that change the behavior of the program.

Take the example below

String value = System.getProperty("foo");
System.out.println("My value is: " + value);

Then when you run the program such as:

java -Dfoo="bar" -jar MyProg.jar

you will see the output as:

My value is: bar

Upvotes: 2

Related Questions