Get Operating System Information in Java using OSHI library

Tags: OSHI OS

Introduction

In this tutorial, we show you how to use OSHI library in a Java program to get operating system information. By different example Java programs we learn how to get information of operating system name, version, manufacturer, etc.

What is OSHI?

OSHI is a free JNA-based (native) Operating System and Hardware Information library for Java. It does not require the installation of any additional native libraries and aims to provide a cross-platform implementation to retrieve system information, such as OS version, processes, memory and CPU usage, disks and partitions, devices, sensors, etc.

For more information about the OSHI library you can visit the library repository at github.com/oshi/oshi

Add OSHI library to the Java project

To use OSHI Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'com.github.oshi', name: 'oshi-core', version: '5.3.4'

To use OSHI Java library in the Maven build project, add the following dependency into the pom.xml file.

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

How to use OSHI to get Operating System Information

To use the OSHI library we need to instantiate a SystemInfo object as this is the main entry point to the OSHI library.

SystemInfo systemInfo = new SystemInfo();

Then get the object of OperatingSystem to retrieve operating system information that your Java program is running on.

OperatingSystem operatingSystem = systemInfo.getOperatingSystem();

Example 1 Get Operating System Information

import oshi.SystemInfo;
import oshi.software.os.OperatingSystem;

public class GetOsInfoExample1 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();

        System.out.println("Operating System: " + operatingSystem.toString());
    }
}
The output is:
Operating System: Microsoft Windows 10.0 (Home) build 19042

Example 2 Get Details of Operating System

import oshi.SystemInfo;
import oshi.software.os.OperatingSystem;

public class GetOsInfoExample2 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();

        System.out.println("Family: " + operatingSystem.getFamily());
        System.out.println("Manufacturer: " + operatingSystem.getManufacturer());
        System.out.println("Number of bits supported by the OS (32 or 64): " + operatingSystem.getBitness());
    }
}
The output is:
Family: Windows
Manufacturer: Microsoft
Number of bits supported by the OS (32 or 64): 64

Example 3 Get Details of Operating System Version

import oshi.SystemInfo;
import oshi.software.os.OperatingSystem;

public class GetOsInfoExample3 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
        OperatingSystem.OSVersionInfo versionInfo = operatingSystem.getVersionInfo();

        System.out.println("Version: " + versionInfo.getVersion());
        System.out.println("CodeName: " + versionInfo.getCodeName());
        System.out.println("Build Number: " + versionInfo.getBuildNumber());
    }
}
The output is:
Version: 10.0
CodeName: Home
Build Number: 19042

Happy Coding 😊

Get CPU Information in Java using OSHI library

Get Physical Memory or RAM Information in Java using OSHI library

Get Virtual Memory Information in Java using OSHI library

Get File System Information in Java using OSHI library