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
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" ) ) {
// ...
}