184

When passing a -D parameter in Java, what is the proper way of writing the command-line and then accessing it from code?

For example, I have tried writing something like this...

if (System.getProperty("test").equalsIgnoreCase("true"))
{
   //Do something
}

And then calling it like this...

java -jar myApplication.jar -Dtest="true"

But I receive a NullPointerException. What am I doing wrong?

3
  • consider using compareToIgnoreCase instead of equalsIgnoreCase for locale-agnostic identifiers; otherwise you may run into the Turkish four-Is problem, among others. Commented Feb 19, 2011 at 10:10
  • 4
    May I suggest using Boolean.getBoolean instead of the long if-statement that you have? shankh.com/2009/07/07/some-fun-with-boolean-getboolean Commented Dec 28, 2011 at 7:31
  • What does -D stands for ? Commented Jul 18, 2019 at 9:03

3 Answers 3

300

I suspect the problem is that you've put the "-D" after the -jar. Try this:

java -Dtest="true" -jar myApplication.jar

From the command line help:

java [-options] -jar jarfile [args...]

In other words, the way you've got it at the moment will treat -Dtest="true" as one of the arguments to pass to main instead of as a JVM argument.

(You should probably also drop the quotes, but it may well work anyway - it probably depends on your shell.)

Sign up to request clarification or add additional context in comments.

4 Comments

Works perfectly now. Also interesting to note is that in order to replicate this behavior within the Eclipse debugger these types of parameters must be placed in the VM Arguments section under Run Configurations.
At least from bash it works with the quotes there (and also allows spaces this way), I use this all the day for ant-calls.
Feel kinda stupid on how much time I spent on that one! Thanks for pointing that out. :)
in case someone wondering, if you want to pass multiple properties just use -D multiple time after a 'space' java -D<key1>=<value1> -D<key2>=<value2> -D<key3>=<value3>...
60

That should be:

java -Dtest="true" -jar myApplication.jar

Then the following will return the value:

System.getProperty("test");

The value could be null, though, so guard against an exception using a Boolean:

boolean b = Boolean.parseBoolean( System.getProperty( "test" ) );

Note that the getBoolean method delegates the system property value, simplifying the code to:

if( Boolean.getBoolean( "test" ) ) {
   // ...
}

1 Comment

last bit is also true for: Integer.getInteger("test"); Long.getLong("test") assuming you have -Dtest=123
32

You're giving parameters to your program instead to Java. Use

java -Dtest="true" -jar myApplication.jar 

instead.

Consider using

"true".equalsIgnoreCase(System.getProperty("test"))

to avoid the NPE. But do not use "Yoda conditions" always without thinking, sometimes throwing the NPE is the right behavior and sometimes something like

System.getProperty("test") == null || System.getProperty("test").equalsIgnoreCase("true")

is right (providing default true). A shorter possibility is

!"false".equalsIgnoreCase(System.getProperty("test"))

but not using double negation doesn't make it less hard to misunderstand.

8 Comments

Actually, System.getProperty("test", "true").equalsIgnoreCase("true") would be the better way to write the last condition.
Boolean.getBoolean("test"); is another option. See.
@Paulo Your solution works for properties only (I wanted to show a general one) but is nicer than mine.
Interesting: In this answer the JVM parameter comes after the -jar flag, while in the other answer it comes after the "java" but before the -jar flag. I take it then that they key is only that the JVM parameter comes before the JAR file itself, in this case "myApplication.jar"?
Thumbs up for proving the point about double negation in such an obvious way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.