Search code examples
javacommand-linesystem-properties

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.


Solution

  • 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