Java Reference
In-Depth Information
Getting Information from System Properties
Problem
You need to get information from the system properties.
Solution
Use
System.getProperty()
or
System.getProperties()
.
Discussion
What is a
property
anyway? A property is just a name and value pair stored in a
java.util.Properties
object, which we discuss more fully in
Storing Strings in Properties
The
System.Properties
object controls and describes the Java runtime. The
System
class
has a static
Properties
member whose content is the merger of operating system specifics
(
os.name
, for example), system and user tailoring (
java.class.path
), and properties
defined on the command line (as we'll see in a moment). Note that the use of periods in these
names (like
os.arch
,
os.version
,
java.class.path
, and
java.lang.version
) makes it
look as though there is a hierarchical relationship similar to that for class names. The
Prop-
erties
class, however, imposes no such relationships: each key is just a string, and dots are
not special.
To retrieve one system-provided property, use
System.getProperty()
. If you want them
all, use
System.getProperties()
. Accordingly, if I wanted to find out if the
System Prop-
erties
had a property named
"pencil_color"
, I could say:
String sysColor
=
System
.
getProperty
(
"pencil_color"
);
But what does that return? Surely Java isn't clever enough to know about everybody's favor-
ite pencil color? Right you are! But we can easily tell Java about our pencil color (or any-
thing else we want to tell it) using the
-D
argument.
The
-D
option argument is used to predefine a value in the system properties object. It must
have a name, an equals sign, and a value, which are parsed the same way as in a properties