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
Commonly Used Constants and Methods
1. Java Version Information
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:
2. Operating System Details
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:
3. User Environment Details
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:
4. File System Properties
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