74

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.

4

5 に答える 5

62

答えはノーです。誰かが次のようなものを設定した例を見たことがあるかもしれません:

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

Arguments次に、アプリケーションはプロパティ文字列の値を解析して個々の値を取得します。あなたの中でmainあなたは次のようにキー値を得ることができます(入力フォーマットが保証されていると仮定して):

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]);
    }
}

また、Javaコマンドラインで-Dメインクラスまたはファイルの前に配置する必要があります。jar例 :java -DArguments=a=1,b=2,c=3,d=4,e=cow MainClass

于 2011-09-16T00:53:27.587 に答える
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

于 2011-09-08T16:48:39.523 に答える
17

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

于 2011-09-08T17:06:25.067 に答える
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

于 2018-01-13T05:27:25.157 に答える
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.

于 2016-08-19T09:55:24.037 に答える