Clik here to view.

1. Overview
In this tutorial, we’ll learn something interesting that could help us know our system. Whenever we think about checking CPU usage, memory status, or disk space in Java, we might ask ourselves, “Do I need to call system commands or use JNI?“ Nope! That’s where OSHI (Operating System and Hardware Information) comes in.
OSHI is a pure Java library that helps us fetch system-level details without any native dependencies. It acts as a bridge between our Java application and system APIs, pulling useful information about the OS, hardware, and network in a cross-platform manner.
2. Why Use OSHI for System Monitoring?
We might wonder, “Why not just use Runtime.exec() or System.getProperty()?” Well, those approaches are limited. System.getProperty() can only fetch the OS name and version, while Runtime.exec() requires OS-specific commands.
OSHI, on the other hand, works with the most popular operating systems (Windows, macOS, Linux). It provides deep system insights about CPU, RAM, disks, sensors, and networks. It doesn’t require native code since it is fully Java-based, and last but not least, it’s lightweight and easy to use – we just need to add a dependency.
3. Key Features and Advantages
With OSHI, we can:
- Get OS details: name, version, architecture, and uptime
- Monitor CPU usage, core details, and processor speed
- Fetch memory statistics: total RAM, available memory, swap usage
- Retrieve disk storage information: partitions, read/write speed
- Track network interfaces, IP addresses, and bandwidth usage
- Access sensor data like CPU temperature, fan speeds, and voltages (if supported)
Now, let’s move further and see how we can implement these features.
4. Setting up OSHI in a Java Project
Before we dive into the fun stuff, let’s get OSHI into our project.
If we’re using Maven, we need to add the oshi-core dependency to our pom.xml:
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>6.4.2</version>
</dependency>
Once added, OSHI will be ready to roll out!
5. Retrieving Basic System Information
Now that OSHI is set up, let’s start with the basics: fetching OS details and checking system uptime.
5.1. Fetching Operating System Details
Think of our OS as the brain of the machine. Wouldn’t it be nice to know what OS we’re running on, which version, and whether it’s 32-bit or 64-bit?
Let’s see how easily we can do that with OSHI:
The method processor.getPhysicalProcessorCount() returns the number of physical CPU cores in the system. It ensures the system has at least one physical core, and processor.getLogicalProcessorCount() returns the number of logical processors, including hyper-threaded cores.
6.2. Measuring CPU Load Dynamically
If we want to check how busy our CPU is at a given moment, we can easily do so:
@Test
void givenSystem_whenUsingOSHI_thenExtractCPULoad() throws InterruptedException {
SystemInfo si = new SystemInfo();
CentralProcessor processor = si.getHardware().getProcessor();
long[] prevTicks = processor.getSystemCpuLoadTicks();
TimeUnit.SECONDS.sleep(1);
double cpuLoad = processor.getSystemCpuLoadBetweenTicks(prevTicks) * 100;
assertTrue(cpuLoad >= 0 && cpuLoad <= 100, "CPU load should be between 0% and 100%");
}
The code captures the system’s CPU load ticks (prevTicks), waits for one second, and calculates the CPU load percentage between the recorded ticks using getSystemCpuLoadBetweenTicks(prevTicks) * 100. Finally, it asserts that the CPU load falls within the valid range of 0% to 100%, ensuring the correctness of the OSHI-reported CPU utilization.
A lower percentage means our CPU is idle, while a higher percentage indicates a high workload.
7. Memory Monitoring
Our system’s RAM (Random Access Memory) determines how many applications can run simultaneously.
7.1. Retrieving Total and Available RAM
Let’s see how we can retrieve the total and available RAM for our system:
@Test
void givenSystem_whenUsingOSHI_thenExtractMemoryDetails() {
SystemInfo si = new SystemInfo();
GlobalMemory memory = si.getHardware().getMemory();
assertTrue(memory.getTotal() > 0, "Total memory should be positive");
assertTrue(memory.getAvailable() >= 0, "Available memory should not be negative");
assertTrue(memory.getAvailable() <= memory.getTotal(), "Available memory should not exceed total memory");
}
The code initializes a SystemInfo object and obtains the GlobalMemory instance, which provides memory-related information. It asserts that the total memory is positive, ensuring the system has valid RAM. It also checks that the available memory is non-negative and doesn’t exceed the total memory, validating OSHI’s memory reporting.
7.2. Storage and Disk Information
@Test
void givenSystem_whenUsingOSHI_thenExtractDiskDetails() {
SystemInfo si = new SystemInfo();
List<HWDiskStore> diskStores = si.getHardware().getDiskStores();
assertFalse(diskStores.isEmpty(), "There should be at least one disk");
for (HWDiskStore disk : diskStores) {
assertNotNull(disk.getModel(), "Disk model should not be null");
assertTrue(disk.getSize() >= 0, "Disk size should be non-negative");
}
}
8. Limitations of OSHI
While OSHI is feature-rich, it does have some limitations:
- Sensor data availability depends on hardware support: Not all machines expose temperature or voltage readings
- Limited low-level control: OSHI provides read-only system insights; it doesn’t allow system modifications
- Dependency on system APIs: Some information may vary slightly between operating systems
9. Conclusion
In this article, we saw that OSHI is a powerful yet lightweight Java library for retrieving system and hardware information. It eliminates the hassle of dealing with native system commands, JNI, or platform-specific dependencies, making it a great choice for developers who need cross-platform system monitoring.
As always, the code presented in this article is available over on GitHub.
The post Introduction to OSHI first appeared on Baeldung.Image may be NSFW.Clik here to view.