Java Reference
In-Depth Information
generally can be hard to determine on modern systems, and almost certainly not where we
would like to keep the data.
What we really want is some way to allow information about the particular environment on
which the JVM is running to be passed into our program. And, in fact, we have such a mech-
anism in the properties that we saw in our last example.
The standard set of properties can be used to tell us about the particular Java virtual machine
that we are using, and some of the file and path conventions of the underlying system. But
we can introduce new properties as well. There are a number of ways to do this. We could
have a properties file included in the meta-information directory of the .jar file that holds our
application. But we simply want to have a property that will tell us where in the filesystem to
place our set of team directories so we can store the Player information. For something this
simple, we will just let whoever is using our application define a new system property on the
command line (or in the script) that launches the program. The java command allows defin-
ing such a system property with the command-line option:
-D propertyName = value
Such a property will be added to the set of system properties that can be found in our program.
So all that is left is to define a name, and then write the code that uses that name. When de-
fining the name, it is important to make sure that the name will be unique, so we will use the
convention of building a dot-separated name that will begin with some identifier for our pro-
gram, followed by the name of the particular property. Unlike the rather long names that are
used with packages to make sure that they are unique, we can use a smaller name for a prop-
erty. Properties need to be unique only within the scope of a single running JVM. Packages,
since they scope the names of classes that may be reused almost anywhere, need to be globally
unique, which requires more effort (and more characters). We will call our program Statist-
ics and the property fileRoot . So on our command line we will need to add the option:
-DStatistics.fileRoot=...
where we replace the ... with the directory in our filesystem where we want to store the stat-
istics. We should also come up with a reasonable default in case the user forgets (or decides
against) supplying a directory. The system properties allow us to find out a couple of likely
candidates, including the user's home directory or the current working directory for the user.
We will use the first of these.
Search WWH ::




Custom Search