I see many Java use -D
option to pass in some parameters.
And the more familiar way is to pass parameters with String[] args
to the main
method.
So what's the difference between these 2 approaches?
Is there any scenario when we must prefer one to the other?
The -D
switch is for setting system properties rather than for passing arguments to your program. In fact the two approaches don't even send information to the same place: the String[]
arguments are sent to your program, whereas the switch gets sent to the virtual machine (from which your program can access the properties if it needs to).
Use arguments unless you have a clear idea of why you need a command line switch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With