javajvmsystem-properties

If I specify a System property multiple times when invoking JVM which value is used?


If I specify a system property multiple times when invoking the JVM which value will I actually get when I retrieve the property? e.g.

java -Dprop=A -Dprop=B -jar my.jar

What will be the result when I call System.getProperty("prop");?

The Java documentation on this does not really tell me anything useful on this front.

In my non-scientific testing on a couple of machines running different JVMs it seems like the last value is the one returned (which is actually the behavior I need) but I wondered if this behavior is actually defined officially anywhere or can it vary between JVMs?


Solution

  • The java.util.System class is backed by a Properties class, which is just an extension of Hashtable. Assuming the values are read in order when passing as arguments to the JVM, then the last value assigned will be the final value.