kub1x
kub1x

Reputation: 3552

Why System.getProperties() returns null for -D defined property?

I'm running a jarball using

java -classpath myBatch.jar some.package.MyMainClass \
     -bloodyArgument "I got the argument!" -Dbloody.prop="I got the prop!"

Then in my main I have:

Properties argsProps = BatchUtils.argsToProperties(args);
System.out.println(argsProps.getProperty("bloodyArgument"));
System.out.println(System.getProperty("bloody.prop"));

And I get output:

I got the argument!
null

I can see the command line bloodyArgument (I added it to see if "something" gets passed to the program), but I'd expect the -D argument to set the system property. Why is the "bloody.prop" null?

PS: BatchUtils.argsToProperties() does what you'd expected it to do: parse -argName "value" from command line into argName=value property pair.

Upvotes: 0

Views: 5044

Answers (1)

kub1x
kub1x

Reputation: 3552

Everything after the class argument of java command gets passed to your main in String[] args and doesn't make it to the JVM.

The solution was to rearrange the properties like this:

java -classpath myBatch.jar -Dbloody.prop="I got the prop!" \
     some.package.MyMainClass -bloodyArgument "I got the argument!" 

I didn't find this anywhere explicitly stated when waddling the web with http://duckduckgo.com or googling this queation of "null -D defined property". I figured it a lot later when printing all system properties and the whole arguments array, so am posting for others.

Upvotes: 3

Related Questions