SystemUtils

About

SystemUtils is a utility class provided by Apache Commons Lang for accessing system-related information such as:

  • Java runtime version

  • Operating system details

  • File system properties

  • User environment details

  • Temporary and user directories

Java’s built-in System.getProperty() works well, but it's verbose and lacks consistency. SystemUtils provides a clean, constant-based, and null-safe way to retrieve environment and system properties.

Characteristics

  • All fields and methods are static.

  • Offers system-level constants for Java and OS properties.

  • Safer alternative to System.getProperty(...).

  • Commonly used in logging, debugging, platform-specific behavior, and testing.

Maven Dependency & Import

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version> <!-- or latest -->
</dependency>
import org.apache.commons.lang3.SystemUtils;

Commonly Used Constants and Methods

1. Java Version Information

Constant
Description

JAVA_VERSION

Java version (e.g., "17" or "1.8.0_321")

JAVA_VERSION_FLOAT

Java version as float

JAVA_VERSION_TRIMMED

Trimmed version (e.g., "17", "11", "1.8")

JAVA_VENDOR

Java vendor (e.g., "Oracle Corporation")

JAVA_VENDOR_URL

Vendor URL

JAVA_HOME

Path to installed Java home

Example:

System.out.println(SystemUtils.JAVA_VERSION); // e.g., "17.0.2"

2. Operating System Details

Constant
Description

OS_NAME

OS name (e.g., "Windows 10")

OS_VERSION

OS version

OS_ARCH

Architecture ("x86", "amd64")

IS_OS_WINDOWS

Boolean flag if Windows

IS_OS_LINUX

Boolean flag if Linux

IS_OS_MAC

Boolean flag if macOS

Example:

if (SystemUtils.IS_OS_WINDOWS) {
    // Windows-specific code
}

3. User Environment Details

Constant
Description

USER_NAME

System username

USER_HOME

Home directory path

USER_DIR

Working directory

TMP_DIR

Temporary directory

LINE_SEPARATOR

OS-specific newline ( or \r)

Example:

String tmpPath = SystemUtils.TMP_DIR;

4. File System Properties

Constant
Description

FILE_SEPARATOR

OS file separator ("/" or "\\")

PATH_SEPARATOR

OS path separator (":" or ";")

LINE_SEPARATOR

OS-specific new line

These help write cross-platform file-handling logic.

Why Use SystemUtils Instead of System.getProperty()?

Concern

System.getProperty()

SystemUtils Advantage

Null safety

Returns null, needs checking

Safe constants and clear names

Readability

"os.name", "java.version" unclear

SystemUtils.OS_NAME is self-explanatory

Constants & flags

Must write logic manually

Provides IS_OS_* flags directly

Duplication

Often repeated keys in code

Centralized and standardized references

Last updated