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?
Java System setProperty() Method The setProperty() method of Java system class sets the property of the system which is indicated by a key.
You can set system properties programmatically by using the java. lang. System. setProperties() method.
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.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With