• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the use of java -D option?  RSS feed

 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends, I found this code in one of the java books but could not understand it properly.
The code goes like this:


Now I understand the static block. But it says I need to run this program from command line like this:

And the result is x = 10. It is written that it is a system property. Now what is a system property? What is the use of java -D option? Can I give something different in command line or do something else with the system property? In plain words it is still not clear to me how this program is executing.
So any help would be appreciated.
Thanks in advance.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the property prefix "App" allows setting class properties--I had no idea you could do that; that's really convenient.

A few naive searches didn't provide any immediately obvious documentation though--I'd like to see some, though.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is in the java tool documentation; it says it "sets a system property." I have never seen it before; maybe somebody else will know more about it.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps it's a property specific for this application only?

Using the -D flag, you can set any property you want. While usually you use it to override the usual system properties, you're not bound to those.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but if the above code runs as-is then there's something going on besides just setting a normal system property.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Sujoy mistyped that. Integer has a few static methods called getInteger that check the system properties*. After all, parseInt() returns an int, not an Integer, so you can't call intValue() on it.


Long and Boolean have similar methods.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bummer; that would have been pretty cool.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Yeah, but if the above code runs as-is then there's something going on besides just setting a normal system property.
It won't compile as0is because of the .intValue after the parseInt invocation.
I corrected that and got a NumberFormatException from that static initialiser.

So I read the thread properly; Rob is correct. You can get that 10 out of the system property with the getInteger method, and you can get other things with the System.getProperty call:
campbell@queeg:~/java$ java -DPropertyFlagDemo.name="Rob" PropertyFlagDemo
The name should read "Campbell" but actually reads "Rob"
Please have a look at the Properties class. And also always use copy-and-paste to quote code.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah; what I'm saying is that if the original code had run w/o explicitly getting the property, i.e. the prefix "App" was significant and allowed injecting values at run time, it would be cool.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Yeah; what I'm saying is that if the original code had run w/o explicitly getting the property, i.e. the prefix "App" was significant and allowed injecting values at run time, it would be cool.
"Cool" meaning dangerous? :wink:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a flame-thrower; I can handle command-line system params ;)
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am taking far greater risks with the link I told Vonique Leary not to read in this thread.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*lol* Having washed a cat (non-toilet method) that was a fun read.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!