Chapter 13. DefaultSources and Configuration Files

Table of Contents


Default program behavior can be configured in one or more files that the command line merely overrides. JSAP provides a registerDefaultSource(DefaultSource) method to support this. At the time of this writing, the only concrete implementation of DefaultSource is PropertyDefaultSource, which reads files in java.util.Properties format.

Multiple DefaultSources may be registered with a JSAP. When a default value is sought, the DefaultSources are checked in the order in which they were registered. The first default value encountered by JSAP is used.


PropertyDefaultSource reads default values from text files consisting of zero or more lines of "key=value"-type assignments. Property names may be parameter IDs, long flags, or short flags for the parameters they address. JSAP interprets them in that order (ID, then long flag, then short flag).

When you create a PropertyDefaultSource, you can tell it not to throw Exceptions. This allows you to specify a number of possible locations for files that don't have to exist.

A typical order in which to register PropertyDefaultSources is:

  1. .yourprogramname in the user's home directory (obtained via System properties)

  2. /etc/yourprogramname.conf on *nix systems

  3. $YOURPROGRAM_HOME/yourprogramname.conf if your program makes use of an environment variable to point to its home directory


If you have any suggestions for a search order example for Windows or Mac, please let me know.


PropertyDefaultSource has not yet been tested with QualifiedSwitches; odds are it won't work as you expect.