Java tutorials home
Reading system properties and environment variables in Java
This page will be of particular interest to you if you have one of the
- how do I find out the system temporary directory in Java?
- how do I find out the local machine's default file format (character encoding, line separator) in Java?
- how do I find out processor information (e.g. CPU type, endianness) in Java?
- how do I find out the Java VM version from a running Java program?
Information such as this can be found in two places:
- environment variables, defined and controlled by the operating system;
- system properties, essentially local to the Java system, which are defined
by the JVM and can potentially be specified on the command line when your Java
application is run.
In general, system properties are more useful for multi-platform applications
because they are more consistent across platforms. Indeed, some system properties are
actually set by the VM on the basis of OS environment variables in order to provide a
more consistent way of reading the relevant information. However, we will look at both
types of properties here: the "raw" environment variables, and Java's system properties.
OS environment variables: System.getenv()
Remember that environment variables are in some sense more "raw" variables defined
and controlled by the operating system. Here are some examples of environment variables
typically set on Windows systems:
|ProgramFiles||The path of the Program Files directory.|
|OS||The name of the base Operating System type (modern versions of Windows are generally Windows_NT).|
|SystemRoot||The base directory in which windows system files are installed (typically C:\Windows).|
|USERNAME||The name of the currently logged in user.|
|USERPROFILE||The user directory of the currently logged in user (e.g. C:\Users\Fred).|
You can retrieve a particular environment variable as follows:
String value = System.getenv("USERNAME");
On the next page, we will look at how to list the environment variables that are set on a given system.
Java system properties: System.getProperty()
As mentioned above, system properties are conceptually similar to environment variables.
But they are essentially local to, and controlled by, the Java system, not by the operating
system. On the surface, individual system properties are retrieved in a very similar way
to environment variables, but using the System.getProperty() method:
String value = System.getProperty("user.dir");
Potentially useful properties that are typically defined include the following.
In general, you can reasonably expect these properties to be defined, irrespective
of the operating system (unlike environment variables, which are OS dependent):
|Java system property||Meaning|
|file.encoding||The default encoding used in text files on the local system.|
|file.separator||The character (or, theoretically, sequence of characters) used to separate directory names making up a path on the local system. On Windows, this is a backslash character, whereas on UNIX-like systems it is a forward slash.|
|java.io.tmpdir||The directory in which temporary files can be created on the local system.|
|line.separator||The character or sequence of characters that generally mark a "new line" in a text file on the local operating system. On UNIX-like systems, this is typically a single ASCII character 10, whereas on Windows systems, this is typically a two-character sequence (ASCII 10 and ASCII 13).|
|os.name||The name of the local operating system, such as "Linux" or "Windows 7".|
|os.version||A more specific version number of the local operating system, such as "6.0" or "2.6.18-stab02".|
|path.separator||The separator generally used to separate lists of paths on that operating system, for example a colon (":"). Note that this is not the directory separator used inside paths.|
|user.country||The ISO code of the operating system's (or local user's) configured country.|
|user.dir||The local directory from which the Java process has been started, and from which files will be read/written by default unless a path is specified.|
|user.home||The current user's "home" directory, such as C:\Users\Fred on Windows systems, or /home/fred/ on UNIX-like systems.|
|user.language||The ISO code of the operating system's (or local user's) configured language, such as "en" for English.|
|user.name||The local user's system user name. On Windows systems, this is typically close to a "real life" name. On UNIX-like systems, it is common for user names to be all lower case letters.|
It should be noted that many of these properties can be implicitly accessed via other API methods. For example, when you construct a File object with no directory specified, it implicitly refers to a file inside user.dir. Simiarly, java.io.tmpdir is implicitly— and more conveniently— read by the API call File.createTemporaryDir().
As with environment variables, on the next page see how to list the Java system properties that are set in a given JVM instance.
Written by Neil Coffey. Copyright © Javamex UK 2010. All rights reserved.