Lolly
Lolly

Reputation: 36432

Passing -D arguments to Java command line as file

I want to pass -D arguments to a Java program, which will set those variables as System Properties. But I have 3 to 4 -D arguments and the list can grow dynamically, so is it possible to add all those arguments into a file and pass it as arguments ?

Default Method

-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

I would like to do it as

variables.dat
-Dfoo="some String"
-Dbar="some string"
      ....
      ....

java -SOME_OPTION variables.dat SomeClass

Is it possible to achieve this ? Where I dint get any help from net. Please help me out.

Upvotes: 1

Views: 2439

Answers (2)

Deepak Singhal
Deepak Singhal

Reputation: 10876

It is very simple to implement. Suppose the file is

-Dkey1=value1
-Dkey2=value2

You have to just read this file; and do in a loop do System.setProperty(key1.substring(2), value1) ; because that is ultimately what -D option do.

Upvotes: 3

Thorn G
Thorn G

Reputation: 12766

No, the java executable will not read this file to parse the properties. You will have to rely on your shell to do something like echoing the properties from the file as part of the arguments to the call to the JVM.

Upvotes: 0

Related Questions