14

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?

oberlies
  • 11,503
  • 4
  • 63
  • 110
  • 5
    You can determine this by simply trying? Can’t you? If what you want is a formal definition, that's a different question. Maybe you should edit yours. – Edwin Dalorzo Apr 24 '14 at 13:15
  • I'm not writing a Java program for myself, so I can't just try this out on all my user's VMs. – oberlies Apr 24 '14 at 13:30
  • You don't need to try it on **all** JVMs. You just need to try it on **one** –  Apr 24 '14 at 13:34
  • 2
    @a_horse_with_no_name This is only the case if the behaviour of the one VM is due to a specification. It could also be an unspecified implementation detail. – oberlies Apr 24 '14 at 13:55

2 Answers2

10

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

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    Actually, the `getProperty` documentation only implies that the value is not `null`. But this is a useful piece of information - and probably the only thing that implementations checking for `-Dflag` flags should rely on. – oberlies Apr 24 '14 at 13:58
  • @oberlies I agree, just better than nothing – Evgeniy Dorofeev Apr 24 '14 at 14:15
  • In "Zulu11.37+17-CA", with `-Dflag`, the `System.getProperty("flag")` returns `null`. – Hong Apr 01 '20 at 09:46
3

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.

oberlies
  • 11,503
  • 4
  • 63
  • 110