Reading system properties and environment variables in Java

This page will be of particular interest to you if you have one of the following questions:

Information such as this can be found in two places:

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:

Environment variableMeaning
ProgramFilesThe path of the Program Files directory.
OSThe name of the base Operating System type (modern versions of Windows are generally Windows_NT).
SystemRootThe base directory in which windows system files are installed (typically C:\Windows).
USERNAMEThe name of the currently logged in user.
USERPROFILEThe 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 propertyMeaning
file.encodingThe default encoding used in text files on the local system.
file.separatorThe 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.tmpdirThe directory in which temporary files can be created on the local system.
line.separatorThe 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.nameThe name of the local operating system, such as "Linux" or "Windows 7".
os.versionA more specific version number of the local operating system, such as "6.0" or "2.6.18-stab02".
path.separatorThe 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.countryThe ISO code of the operating system's (or local user's) configured country.
user.dirThe 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.homeThe current user's "home" directory, such as C:\Users\Fred on Windows systems, or /home/fred/ on UNIX-like systems.
user.languageThe ISO code of the operating system's (or local user's) configured language, such as "en" for English.
user.nameThe 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.


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.