Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways to pass parameters: String args[] or -D? [closed]

Tags:

java

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?

ADD 1

Is there any scenario when we must prefer one to the other?

like image 437
smwikipedia Avatar asked Mar 18 '23 14:03

smwikipedia


1 Answers

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.

like image 88
chiastic-security Avatar answered Apr 07 '23 17:04

chiastic-security