Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct for Java system properties to be used to set and get arbitrary program parameters?

Tags:

java

It is possible using -Dproperty=value arguments to set arbitrary system properties (not some fixed set of system properties actually used by the JVM) and a program can get these properties later using System.getProperty("property"). Is it correct to do this?

I haven't found an authoritative answer on this, so that's why I'm asking here. It seems to me that program parameters should be set through command line arguments to the program, not to the JVM. However, perhaps this is an accepted practice that just isn't documented anywhere I've looked so far. I'd like to be sure. Thanks.

like image 787
Anonymous Avatar asked Jan 23 '12 09:01

Anonymous


2 Answers

I think Java system properties are used to pass values from command line to libraries or plugins inside the execution. It is, that insider component has no direct way to receive the parameter from the main program that's executing it. So it reads it from a "context" that Java system properties are.

If we look at it as layers, command line arguments would be parameters for the inmediate lower layer, and system java properties are context for all the lower layers.

command line: arguments + properties
   main program: uses arguments
      some library/plugin: uses properties from context

If it's not this way the main program should have to carry all the parameters that user could set to the lower layers and it can be very cumbersome.

I don't like to depend on contextual properties so if I design a program I'd try to pass over all the properties in some non-global way. But it can be very handy some times (and using namespacing it's not probable they collide).

like image 158
helios Avatar answered Oct 14 '22 12:10

helios


In my opinion this is not "incorrect" and there are programs and libraries that do use their own system properties for configuration.

However, it is probably more practical to put configuration parameters for your software in a configuration file (in whatever format you think is suitable - a .properties file, an XML file, or something else). It is cumbersome, especially if you have many configuration parameters, to have to put all those parameters on the command line with -Dname=value options.

like image 41
Jesper Avatar answered Oct 14 '22 12:10

Jesper