Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 658
RobV Avatar asked Jun 08 '12 22:06

RobV


People also ask

What is JVM system property?

System properties contain information to configure the JVM and its environment. Some system properties are particularly relevant for JVMs in a CICS® environment. Specify JVM system properties in the JVM profile.

How do you pass system properties in Java?

You can pass Java system properties to Microservices Runtime by modifying the setenv. bat/sh file.

Where are JVM properties set?

The directory server provides a means of configuring the Java Virtual Machine (JVM) and Java options for each command-line utility and for the directory server itself. The Java configuration is provided in a properties file, located at instance-dir /OUD/config/java.

What are the JVM parameters?

Now let's discuss the most frequently used JVM Parameters which are 3 namely as follows: Java Heap Size. Garbage Collector. Print GC.


1 Answers

There's nothing like writing a little class to see how it works.

public class PropTest {    public static void main(String[] args) {     System.out.println(System.getProperty("prop"));   }  } 

Which when compiled and ran with the command line

java -Dprop=A -Dprop=B -Dprop=C PropTest 

yeilds the output

C 

Which would imply that the values are put into the table left to right, with the last value overwriting previous values.

Just to make a note of the environment, Fedora 16, Linux 3.3.7, 64 bit

> java -version  java version "1.6.0_24" OpenJDK Runtime Environment (IcedTea6 1.11.1) (fedora-65.1.11.1.fc16-x86_64) OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode) 
like image 169
Edwin Buck Avatar answered Sep 21 '22 10:09

Edwin Buck