Operations 14 min read

Getting Started with OSHI: A Java Library for Cross‑Platform System Information

This article introduces OSHI, a free open‑source Java library that uses JNA to provide cross‑platform hardware and operating‑system details, outlines its main features, advantages, and limitations, and supplies complete Maven dependency and Java code examples for building system‑monitoring tools.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Getting Started with OSHI: A Java Library for Cross‑Platform System Information

1. What Is OSHI?

OSHI (Operating System and Hardware Information) is a free open‑source Java library that accesses native system APIs via JNA, allowing it to run on Windows, macOS, Linux, Solaris and other platforms without compiling native code.

1.1 Main Features

Cross‑platform support (Windows, macOS, Linux, Solaris, etc.)

Pure Java implementation using JNA, no external binaries required

Lightweight and easy to integrate into existing Java applications

Active GitHub community with frequent updates

1.2 Advantages

No external binaries or platform‑specific agents – just add the dependency

Unified API abstracts OS‑specific nuances

Ideal for monitoring dashboards, log agents, and health‑check modules

Built‑in support for CPU usage, memory, disk I/O, network interfaces, etc.

1.3 Limitations

Performance overhead due to JNA compared with native APIs

API variability because it depends on underlying OS APIs that may differ across versions

Read‑only access – cannot perform control actions such as restarting services

1.4 Why Use OSHI for System Monitoring?

Simplicity – eliminates the need for platform‑specific tools like top , vmstat or iostat

Integrability – can be embedded in Spring Boot apps, JavaFX dashboards, or CLI tools

Extensibility – provides detailed information on CPU, memory, disks, network interfaces, processes, sensors, etc.

2. Code Example

This section shows how to add OSHI to a Java project, implement system‑monitoring logic, and interpret the output.

2.1 Add Dependency (pom.xml)

<dependency>
    <groupId>com.github.oshi</groupId>
    <artifactId>oshi-core</artifactId>
    <version>latest__jar__version</version>
</dependency>

2.2 Java Code

The following Java program demonstrates how to retrieve and display comprehensive system information using OSHI.

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HWDiskStore;
import oshi.hardware.NetworkIF;
import oshi.hardware.Sensors;
import oshi.software.os.OperatingSystem;
import oshi.software.os.OSProcess;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * OshiSystemMonitor demonstrates how to use OSHI to retrieve and display detailed system information.
 */
public class OshiSystemMonitor {
    public static void main(String[] args) throws InterruptedException {
        // Initialize SystemInfo
        SystemInfo systemInfo = new SystemInfo();
        OperatingSystem os = systemInfo.getOperatingSystem();
        ComputerSystem cs = systemInfo.getHardware().getComputerSystem();
        System.out.println("=== Basic System Info ===");
        System.out.println("OS: " + os);
        System.out.println("Manufacturer: " + cs.getManufacturer());
        System.out.println("Model: " + cs.getModel());
        System.out.println("Serial Number: " + cs.getSerialNumber());

        // CPU information
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        System.out.println("\n=== CPU Info ===");
        System.out.println("CPU: " + processor.getProcessorIdentifier().getName());
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        TimeUnit.SECONDS.sleep(1);
        double cpuLoad = processor.getSystemCpuLoadBetweenTicks(prevTicks) * 100;
        System.out.printf("CPU Load: %.2f%%\n", cpuLoad);

        // Memory information
        GlobalMemory memory = systemInfo.getHardware().getMemory();
        long total = memory.getTotal();
        long available = memory.getAvailable();
        long used = total - available;
        System.out.println("\n=== Memory Info ===");
        System.out.println("Total: " + (total / 1024 / 1024) + " MB");
        System.out.println("Used: " + (used / 1024 / 1024) + " MB");
        System.out.println("Free: " + (available / 1024 / 1024) + " MB");

        // Disk information
        System.out.println("\n=== Disk Info ===");
        List<HWDiskStore> diskStores = systemInfo.getHardware().getDiskStores();
        for (HWDiskStore disk : diskStores) {
            System.out.println("Disk Name: " + disk.getName());
            System.out.println("Model: " + disk.getModel());
            System.out.println("Serial: " + disk.getSerial());
            System.out.println("Size: " + (disk.getSize() / 1024 / 1024 / 1024) + " GB");
            System.out.println("Reads: " + disk.getReads());
            System.out.println("Writes: " + disk.getWrites());
            System.out.println("Read Bytes: " + disk.getReadBytes());
            System.out.println("Write Bytes: " + disk.getWriteBytes());
            System.out.println();
        }

        // Network interface information
        System.out.println("\n=== Network Interfaces ===");
        List<NetworkIF> networkIFs = systemInfo.getHardware().getNetworkIFs();
        for (NetworkIF net : networkIFs) {
            net.updateAttributes();
            System.out.println("Name: " + net.getName());
            System.out.println("Display Name: " + net.getDisplayName());
            System.out.println("MAC: " + net.getMacaddr());
            System.out.println("IPv4: " + String.join(", ", net.getIPv4addr()));
            System.out.println("IPv6: " + String.join(", ", net.getIPv6addr()));
            System.out.println("Bytes Sent: " + net.getBytesSent());
            System.out.println("Bytes Received: " + net.getBytesRecv());
            System.out.println("Packets Sent: " + net.getPacketsSent());
            System.out.println("Packets Received: " + net.getPacketsRecv());
            System.out.println();
        }

        // Top 5 processes by CPU usage
        System.out.println("\n=== Top 5 CPU Processes ===");
        List<OSProcess> procs = os.getProcesses(5, OperatingSystem.ProcessSort.CPU);
        for (OSProcess proc : procs) {
            System.out.printf("PID: %d, Name: %s, CPU: %.2f%%, Memory: %d MB\n",
                proc.getProcessID(), proc.getName(), 100 * proc.getProcessCpuLoadCumulative(), proc.getResidentSetSize() / 1024 / 1024);
        }

        // Sensor information
        Sensors sensors = systemInfo.getHardware().getSensors();
        System.out.println("\n=== Sensor Info ===");
        System.out.println("CPU Temperature: " + sensors.getCpuTemperature() + " °C");
        System.out.println("CPU Fan Speed: " + sensors.getFanSpeeds()[0] + " RPM (if available)");
        System.out.println("CPU Voltage: " + sensors.getCpuVoltage() + " V");
    }
}

2.2.1 Code Explanation

The program creates a SystemInfo object, prints basic OS details, measures CPU load over a one‑second interval, reports memory usage, iterates over physical disks to show size and I/O statistics, lists network interfaces with addresses and traffic counters, displays the top five processes by CPU consumption, and finally outputs sensor data such as temperature, fan speed, and voltage.

2.2.2 Sample Output

=== Basic System Info ===
OS: Windows 11 build 22621
Manufacturer: Dell Inc.
Model: XPS 15 9500
Serial Number: ABC123XYZ

=== CPU Info ===
CPU: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
CPU Load: 12.34%

=== Memory Info ===
Total: 16384 MB
Used: 7312 MB
Free: 9072 MB

=== Disk Info ===
Disk Name: \\.\PhysicalDrive0
Model: Samsung SSD 970 EVO 1TB
Serial: S3Z6NX0M123456
Size: 931 GB
Reads: 123456
Writes: 654321
Read Bytes: 9876543210
Write Bytes: 1234567890

=== Network Interfaces ===
Name: eth0
Display Name: Ethernet Adapter
MAC: 00-1A-2B-3C-4D-5E
IPv4: 192.168.1.10
IPv6: fe80::1a2b:3c4d:5e6f:7a8b
Bytes Sent: 1234567
Bytes Received: 7654321
Packets Sent: 12345
Packets Received: 54321

=== Top 5 CPU Processes ===
PID: 1234, Name: chrome.exe, CPU: 15.67%, Memory: 450 MB
PID: 5678, Name: java.exe, CPU: 10.23%, Memory: 1200 MB
PID: 9101, Name: explorer.exe, CPU: 5.45%, Memory: 200 MB
PID: 1121, Name: code.exe, CPU: 3.89%, Memory: 350 MB
PID: 3141, Name: slack.exe, CPU: 2.15%, Memory: 150 MB

=== Sensor Info ===
CPU Temperature: 55.0 °C
CPU Fan Speed: 1200 RPM (if available)
CPU Voltage: 1.2 V

3. Summary

OSHI is a powerful, developer‑friendly Java library for retrieving system information; it is well‑suited for building monitoring tools, log agents, or health‑check dashboards, offering cross‑platform support, zero native compilation, and a comprehensive set of metrics.

javacross‑platformsystem monitoringJNAHardware InfoOSHI
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.