Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set multiple system properties Java command line

Is there an easier way to specify multiple System Properties on the command line to a Java program rather than having multiple -D statements?

Trying to avoid this:

 java -jar -DNAME="myName" -DVERSION="1.0" -DLOCATION="home" program.jar 

I thought I had seen an example of someone using one -D and some quoted string after that, but I can't find the example again.

like image 662
Tyler DeWitt Avatar asked Sep 08 '11 16:09

Tyler DeWitt


People also ask

How do I set Java system properties in Windows?

System properties are set on the Java command line using the -Dpropertyname=value syntax. They can also be added at runtime using System. setProperty(String key, String value) or via the various System. getProperties().

How do I set system properties in Linux?

Go to the Registry Editor (Start > regedit.exe). To change existing properties double-click the appropriate value. To change additional properties, double-click options. Refer to the list of parameters in Recognized System Properties.


2 Answers

Answer is NO. You might have seen an example where somebody would have set something like :

-DArguments=a=1,b=2,c=3,d=4,e=cow

Then the application would parse value of Arguments property string to get individual values. In your main you can get the key values as(Assuming input format is guaranteed):

String line = System.getProperty("Arguments"); if(line != null) {   String str[] = line.split(",");     for(int i=1;i<str.length;i++){         String arr[] = str[i].split("=");         System.out.println("Key = " + arr[0]);         System.out.println("Value = " +  arr[1]);     } } 

Also, the -D should be before the main class or the jar file in the java command line. Example : java -DArguments=a=1,b=2,c=3,d=4,e=cow MainClass

like image 174
ring bearer Avatar answered Sep 21 '22 08:09

ring bearer


There's nothing on the Documentation that mentions about anything like that.

Here's a quote:

-Dproperty=value Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:

java -Dfoo="some string" SomeClass

like image 33
adarshr Avatar answered Sep 22 '22 08:09

adarshr