Set multiple system properties Java command line
Asked Answered
H

5

83

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.

Highway answered 8/9, 2011 at 16:39 Comment(1)
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.Diploblastic
K
66

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

Killough answered 16/9, 2011 at 0:53 Comment(2)
Also, the -D should be before the main class or the jar file Thanks! You save me.Intrinsic
Arguments can come after the -jar if you use --key=value instead of -Dkey-value.Tallbot
I
23

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

Ineffectual answered 8/9, 2011 at 16:48 Comment(5)
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 meMcelroy
@Mcelroy 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.Ineffectual
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?Mcelroy
@Mcelroy 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 propertiesIneffectual
Thank you :) I appreciate your helpMcelroy
A
17

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

Amye answered 8/9, 2011 at 17:6 Comment(1)
+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.Dikdik
C
9

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

Cephalothorax answered 13/1, 2018 at 5:27 Comment(0)
L
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.

Lahnda answered 19/8, 2016 at 9:55 Comment(1)
How is using the -D approach require a new build for a single property?Aqualung

© 2022 - 2024 — McMap. All rights reserved.