Presents your JAVA E-NEWSLETTER for August 29, 2002 <-------------------------------------------> MAKE COMMAND-LINE ARGUMENTS THE EASY WAY You can easily get a simple command-line program with a lot of power by using the Java standard way of passing flags. In fact, the flags passed are available from more than just the main method. The java command will pass any value set with the -D flag straight into the System class' Properties object. This allows the flag to be checked by calling System.getProperty. Running the following code with java -Dflag=jack will output "jack" to the screen. public class ShowFlag { static public void main(String[] args) { String flag = System.getProperty("flag"); System.err.println(flag); } } To see all the properties, you can run the following: import java.util.Properties; public class ShowAll { static public void main(String[] args) { Properties sysprops = System.getProperties(); sysprops.list(System.err); } } When passing in new system properties, it is common to style them in reversed domain name, such as: java -Dcom.generationjava.someproduct.color=green rather than: java -Dcolor=green This technique prevents your flag from clashing with another passed system property. While this is a nice trick for simple classes, a full-on Java command-line program really needs to use a proper argument parser. The argument parser can help verify whether particular flags are allowed. It also allows for simple one character flags and automatic conversion of values. ----------------------------------------