Table of Contents
Introduction
One of the things that varies between operating systems is how new lines are handled within files or console/terminal output. If we need to produce file based output, how then can we make an application that doesn’t care which operating system we are running upon? We can use a Java-provided system property in our code.
Retrieving a System Property
If we want to retrieve a system property, such as the end-of-line marker (AKA the line.separator property), we could store it in a variable like:
private String lineSeparator = System.getProperty("line.separator");
Then, anywhere we previously would hard-code \n (newline) or other such end-of-line characters, we could instead use this variable value. Remember, this also includes your tests, we don’t want to falsely limit ourselves to only running on one platform by our unit tests.
Examples of Java System Properties
Some examples of Java system properties are:
| Property Name | Description | Example Value(s) |
|---|---|---|
| file.encoding | The character set used for files saved to the file system. | UTF-8 |
| file.separator | The character used to separate portions of a file path, such as folder and file name. | UNIX: / Windows: \ |
| java.class.path | Various directories from which jar archives containing Java code will be retrieved. See also: path.separator. | |
| java.home | The directory where the Java Runtime Environment (JRE) is installed. | |
| java.vendor | The company that created the Java Runtime Environment (JRE). | |
| java.vendor.url | A URL for the company that created the Java Runtime Environment (JRE). | https://java.oracle.com/ |
| java.version | The version of the Java Runtime Environment (JRE). | 20 |
| line.separator | The character(s) used by the operating system to signal that one line is ending and the subsequent text (if any) should be displayed on the next line. | \n \r\f |
| native.encoding | The file encoding used by default in the operating system. | Cp1252 |
| os.arch | The operating system architecture name. | |
| os.name | The operating system name. | Windows 10 |
| os.version | The version of the operating system. | |
| path.separator | The character used to separate different values within the path. See also: java.class.path. | ; |
| user.dir | The current (working) directory for the application. | C:\Users\Marcy\java101 |
| user.home | The home directory for the user running the Java application. | C:\Users\Marcy |
| user.name | The name of the user’s account running the Java application. | Marcy |
Summary
We can use Java’s system properties to abstract away some of the operating system-specific logic that would otherwise need to be tracked in our applications, such as:
- End-of-line characters
- File encodings,
- The home directory of the current user,
- The working directory for the application
This way, we are less likely to need operating system (OS) specific logic within our applications. It isn’t absolute. For example a Java-based User Interface (UI) might still need an OS-specific look and feel. However, for backend Java code, these system properties help reduce the complexity of our application’s logic when we want to support different operating systems.

Leave a comment