What is a property anyway? A property is just a name and value pair
stored in a java.util.Properties
object, which
we’ll discuss more fully in Section 7.8. So if
I chose to, I could store the following properties in a
Properties
object called ian
:
name=Ian Darwin favorite_popsicle=cherry favorite_rock group=Fleetwood Mac favorite_programming_language=Java pencil color=green
The Properties
class has several forms of its retrieval
method. You could, for example, say
ian.getProperty("pencil
color")
and get back the string “green”. You can also provide a
default: say ian.getProperty("pencil color", "black"),
and if the property has not been set you would
get the default value “black”.
For now, we’re concerned with the
System
class
and its role as keeper of the particular
Properties
object that 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
and java.class.path
,
java.lang.version
) makes it look as though there
is a hierarchical relationship similar to that for class names. The
Properties
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
Properties
had a property named “pencil
color”, I could say:
String color = System.getProperty("pencil color");
But what will that return? Surely Java isn’t clever enough to
know about everybody’s favorite pencil color? Right you are!
But we can easily tell Java about our pencil color (or anything 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 file (see below). You can have more than one
-D
definition after your class name on the Java
command. On
Unix or MS-Windows command-line mode,
use this:
java -D"pencil color=Deep Sea Green" SysPropDemo
Using MRJ or an IDE, put the variable’s name and value in the
appropriate dialog box when running the program. The
SysPropDemo
program is short; its essence is this
one line:
System.getProperties( ).list(System.out);
When run this way, the program prints around 50 lines, looking something like:
java.library.path=/usr/local/linux-jdk1.2/jre/lib/i386/... java.vm.specification.vendor=Sun Microsystems Inc. sun.io.unicode.encoding=UnicodeLittle pencil color=Deep Sea Green file.encoding=ANSI_X3.4-1968 java.specification.vendor=Sun Microsystems Inc. user.language=en
The program also has code to extract just one or a few properties, so you can say:
$ java SysPropDemo os.arch os.arch = x86
The
Javadoc page for
java.util.Properties
lists the exact rules used in
the load( )
method, as well as other details.
Section 7.8 lists more details on using and naming
your own Properties
files.
3.137.41.205