Java: using system parameters vs "regular" command line options

One of the main advantages of system properties is that they are available at any time during the life of you program.

Command line arguments are only available in the main method (unless you persist them).


I'd argue that the advantage Fortyrunner cites is actually the most significant negative for system properties--they are available to anyone who asks for them.

If the flag or option is meant to be a command-line option, it should be available to the layer or module of your code that deals with taking input from the command line, not any code that asks for it.

You can get some destructive coupling from global state, and system properties are no different than any other global state.

That said, if you're just trying to make a quick and dirty CLI program, and separation of concerns and coupling is not a big concern for you, system properties give you an easy method that however leads to (IMO) poor user experience. Some getopt library will give you a lot more support for building a good CLI user experience.

Tags:

Java