Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a Java system property is set on the command line without value ("-Dkey"), what value does it get?

According to the Oracle documentation, I can set system properties of a Java process on the command line with the following syntax:

-Dproperty=value

But what happens when I don't specify the value, i.e. when I omit the "equals value" part:

-Dproperty

What value will the system property be set to? true? An empty string? Or any string with an undefined, implementation specific value?

like image 593
oberlies Avatar asked Apr 24 '14 13:04

oberlies


People also ask

What is system set property in Java?

Java System setProperty() Method The setProperty() method of Java system class sets the property of the system which is indicated by a key.

Where we can set system property in Java?

You can set system properties programmatically by using the java. lang. System. setProperties() method.

How do I get system properties in Java?

To get a specific system property you can use System. getProperty(String key) or System. getProperty(String key, String def) . Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.

What is key and value in system properties in Java?

The key is the name (key) of the property. The value of the property corresponding to the key in System properties. The function returns String. In this example, we will set a property with key "x" and value "y" using setProperty () method. After setting the property, we will print all the System properties, if our key-value pair is this set.

What is setProperty () method of Java system class?

The setProperty () method of Java system class sets the property of the system which is indicated by a key. key - It is the name of the system property. value - It is the value of the system property. This method returns the System property's previous value.

How to read system properties in Java?

The System class has two methods used to read system properties: getProperty and getProperties. The System class has two different versions of getProperty. Both retrieve the value of the property named in the argument list.

What is getproperties in Java?

getProperties: The java.lang.System.getProperties() method determines the current system properties. getProperty(String key) : java.lang.System.getProperty(String key) method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.


2 Answers

From simple trials with a Oracle HotSpot VM, I can see that system properties set on the command line without value get an empty string as value.

However this is only a partial answer to the question. A link to the some specification would be a better answer.

like image 176
oberlies Avatar answered Oct 25 '22 19:10

oberlies


It will return an empty string. According to System.getProperty(String key) null is returned only if there is no property with that key. So if we define a propety with -D it exists in the system

like image 34
Evgeniy Dorofeev Avatar answered Oct 25 '22 19:10

Evgeniy Dorofeev