71

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.

Captain Man
  • 5,651
  • 3
  • 41
  • 64
Tyler DeWitt
  • 22,191
  • 36
  • 108
  • 189
  • 7
    Starting in Java 9, you can put some or all of those options (e.g., `-DNAME="myName" -DVERSION="1.0" -DLOCATION="home" -jar program.jar`) into a plain text file, and then run the java launcher with the "argument files" syntax, e.g., `java @that_plain_text_file`. – Ti Strga Jul 17 '18 at 19:20

5 Answers5

59

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

ring bearer
  • 18,848
  • 7
  • 53
  • 68
20

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

adarshr
  • 57,189
  • 21
  • 133
  • 158
  • What class does `SomeClass` is referring to? I'm trying to set this property to read a `.properties` file, using Spring, Struts and Tomcat 8.5, that's the only part of the command that's still unclear to me – Frakcool Dec 11 '17 at 23:20
  • @Frakcool `SomeClass` would be the class with the main method that you are trying to run. Typically it would have a package declaration and you should use the fully qualified class name to run it. – adarshr Dec 12 '17 at 00:30
  • Oh I get it, it was what I was thinking, however in a web application there's no `main` method, so, I think it's done in the tomcat's properties, am I right? – Frakcool Dec 12 '17 at 00:33
  • @Frakcool there are a number of ways to set system properties. This question was about setting them from the command line. You can also look into setting the `JAVA_OPTS` environment variable with the required system properties – adarshr Dec 12 '17 at 03:18
  • Thank you :) I appreciate your help – Frakcool Dec 12 '17 at 03:19
16

Instead of passing the properties as an argument, you may use a .properties for storing them.

Dimitri
  • 7,489
  • 18
  • 63
  • 117
  • 5
    +1: You can also load them into the system properties with `System.getProperties().load(new FileInputStream("my.properties"))` However having your own Properties is likely to be a better approach. – Peter Lawrey Sep 08 '11 at 18:02
6

You may be able to use the JAVA_TOOL_OPTIONS environment variable to set options. It worked for me with Rasbian. See Environment Variables and System Properties which has this to say:

In many environments, the command line is not readily accessible to start the application with the necessary command-line options.

This often happens with applications that use embedded VMs (meaning they use the Java Native Interface (JNI) Invocation API to start the VM), or where the startup is deeply nested in scripts. In these environments the JAVA_TOOL_OPTIONS environment variable can be useful to augment a command line.

When this environment variable is set, the JNI_CreateJavaVM function (in the JNI Invocation API), the JNI_CreateJavaVM function adds the value of the environment variable to the options supplied in its JavaVMInitArgs argument.

However this environment variable use may be disabled for security reasons.

In some cases, this option is disabled for security reasons. For example, on the Oracle Solaris operating system, this option is disabled when the effective user or group ID differs from the real ID.

See this example showing the difference between specifying on the command line versus using the JAVA_TOOL_OPTIONS environment variable.

screenshot showing use of JAVA_TOOL_OPTIONS environment variable

Richard Chambers
  • 14,509
  • 3
  • 62
  • 86
0

If the required properties need to set in system then there is no option than -D But if you need those properties while bootstrapping an application then loading properties through the properties files is a best option. It will not require to change build for a single property.

Koray Tugay
  • 20,438
  • 37
  • 155
  • 276