Tag Archives: java

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 :/

Updating your old SCJP X to the new shiny OCPJP 7

As you can read here, the only thing you need to do (if you have one of the old SCJP’s) is an upgrade exam (1Z0-805), focused mostly on the new bits added on Java 7. I’ve created a repository on Github with code examples that I’ve been gathering during my preparation.

upgrade Path

upgrade Path

I’ll upload as well at some point a pdf with some study annotations that I think may be useful to others.

I know plenty of you are very sceptical about certifications but I still think they are quite useful. If not per se, the process (meaning the study + work) you need to go through to get them it certainly is. I’ll be writing a post on this at some point as well.

Eclipse template to insert test methods should-given-when-then

(originally posted on StackOverflow)
I saw a similar version to this one recently while pair programming with a very a good developer and friend, and I think it could be a nice addition to this list.

This template will create a new test method on a class, following the Given – When – Then approach from BDD paradigm on the comments, as a guide for structuring the code. It will start the method name with “should” and let you replace the rest of the dummy method name “CheckThisAndThat” with the best possible description of the test method responsibility. After filling the name, TAB will take you straight to the // Given section so you can start typing your preconditions.

I have it mapped to the three letters “tst”, with description “Test methods should-given-when-then” 😉

I hope you find it as useful as I did when I saw it:

@Test
public void should${CheckThisAndThat}() {
    Assert.fail("Not yet implemented");
    // Given
    ${cursor}

    // When

    // Then

}${:import(org.junit.Test, org.junit.Assert)}