gaybro8777 / java-opts-101

Home Page:http://www.macluq.com/2014/03/11/java_opts-how-to-use-them/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Different possible ways of running this class and the results:

Doing it right, using JAVA_OPTS:

computer$ JAVA_OPTS="-Dproperty1=value1 -Dproperty2=value2 -Dproperty3=value3"
computer$ java $JAVA_OPTS JavaOpts101
Property 1: value1
Property 2: value2
Property 3: value3

Doing it right, without grouping them in JAVA_OPTS:

computer$ java -Dproperty1=value1 -Dproperty2=value2 -Dproperty3=value3 JavaOpts101
Property 1: value1
Property 2: value2
Property 3: value3

Doing it right, grouping two and passing the third one:

computer$ JAVA_OPTS="-Dproperty1=value1 -Dproperty2=value2"
computer$ java $JAVA_OPTS -Dproperty3=value3 JavaOpts101
Property 1: value1
Property 2: value2
Property 3: value3

So how do you do it wrong? Basically, you need to be passing the properties in the wrong place, like this:

computer$ JAVA_OPTS="-Dproperty1=value1 -Dproperty2=value2"
computer$ java $JAVA_OPTS JavaOpts101 -Dproperty3=value3
Property 1: value1
Property 2: value2
Property 3: null

Even if they are grouped on JAVA_OPTS, they won't be correctly passed unless they go before the class name...

computer$ JAVA_OPTS="-Dproperty1=value1 -Dproperty2=value2 -Dproperty3=value3"
computer$ java JavaOpts101 $JAVA_OPTS
Property 1: null
Property 2: null
Property 3: null

A little bit of documentation:

If you do man java on your command-line and have a read, apart from discovering a new spelling for impletmentations (sic), you'll get something along this:

SYNOPSIS
java [ options ] class [ argument... ]

java [ options ] -jar file.jar [ argument... ]

OPTIONS

-Dproperty=value Sets a system property value.

A couple of caveats:

Notice that you only need to use the quotes when you group the system properties under an environment variable (in these examples, JAVA_OPTS), but you don't need to use the quotes when the properties are passed directly on the command line. It may be worthy to note here: the order in which the properties are passed among themselves is irrelevant.