Heart containing Coding Chica Java 101

Carriage Returns, Line Feeds, and New Lines, Oh My! Java System Properties

TIP: References Quick List

Table of Contents

  1. Table of Contents
  2. Introduction
  3. Retrieving a System Property
  4. Examples of Java System Properties
  5. Summary

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 NameDescriptionExample Value(s)
file.encodingThe character set used for files saved to the file system.UTF-8
file.separatorThe character used to separate portions of a file path, such as folder and file name.UNIX: /
Windows: \
java.class.pathVarious directories from which jar archives containing Java code will be retrieved. See also: path.separator.
java.homeThe directory where the Java Runtime Environment (JRE) is installed.
java.vendorThe company that created the Java Runtime Environment (JRE).
java.vendor.urlA URL for the company that created the Java Runtime Environment (JRE).https://java.oracle.com/
java.versionThe version of the Java Runtime Environment (JRE).20
line.separatorThe 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.encodingThe file encoding used by default in the operating system.Cp1252
os.archThe operating system architecture name.
os.nameThe operating system name.Windows 10
os.versionThe version of the operating system.
path.separatorThe character used to separate different values within the path. See also: java.class.path.;
user.dirThe current (working) directory for the application.C:\Users\Marcy\java101
user.homeThe home directory for the user running the Java application.C:\Users\Marcy
user.nameThe 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.

Carriage Returns, Line Feeds, and New Lines, Oh My! Java System Properties

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.