Tag Archives: JDK

JAVA_OPTS, how to use them

For most of you this is probably a very silly post, but after five years of experience I was still shocked by something about the infamous JAVA_OPTS, also known as the “-D” system properties… -Dkey=value

JAVA_OPTS is only the name under which some applications, servers, etc… group a few of these -Dkey=value properties to be passed into Java when it gets executed.

You can always pass them directly from the command-line or whatever script you’re using to trigger the execution of your program. Even combine some of them inside a JAVA_OPTS environment variable and some others outside of it.

But there are certain rules you must respect when using them, as I learnt today. I wrote a silly Java class which prints out the values for three properties that it expects with certain names, and I called this class on many different manners.

This is the (simplest) class (ever):

public class JavaOpts101 {
    public static void main(String[] args) {
        System.out.println("Property 2: " + System.getProperty("property1"));
        System.out.println("Property 2: " + System.getProperty("property2"));
        System.out.println("Property 3: " + System.getProperty("property3"));
    }
}

These are 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.

The code is on Github, even if it’s completely silly :/