System Properties


You need to get information from the system properties.


Use System.getProperty( ) or System.getProperties( ).


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_rock group=Fleetwood Mac
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 (, 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: ...

Get Java Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.