Get CPU Information in Java using OSHI library

Introduction

In this tutorial, we explore how to use the OSHI library in Java program to get CPU details information. Via the OSHI APIs we show how to get all information of a Central Processing Unit (CPU) such as name, number of cores, vendor, model, family, 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 CPU Information

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

SystemInfo systemInfo = new SystemInfo();

The SystemInfo class provides getHardware() method to return HardwareAbstractionLayer object where the Java program can access hardware items such as processors, memory, battery, and disks on the machine that the program is running on.

HardwareAbstractionLayer hardware = systemInfo.getHardware();

To get the CPU information we can access the getProcessor() to get CentralProcessor object. And the CentralProcessor.ProcessorIdentifier to access CPU’s identifier.

CentralProcessor processor = hardware.getProcessor();

CentralProcessor.ProcessorIdentifier processorIdentifier = processor.getProcessorIdentifier();

Example 1 Get all information of CPU as a String

Following Java program to show how to get all information from the processor on the machine as a String.

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;

public class ProcessorInfoExample1 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        CentralProcessor processor = hardware.getProcessor();

        System.out.println(processor.toString());
    }
}
The output is:
Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
 1 physical CPU package(s)
 6 physical CPU core(s)
 12 logical CPU(s)
Identifier: Intel64 Family 6 Model 158 Stepping 10
ProcessorID: BBBBFFFF000906EE
Microarchitecture: Coffee Lake

Example 2 Get details of CPU’s identifier

In the Java example below we show how to get details of the CPU’s identifier.

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;

public class ProcessorInfoExample2 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        CentralProcessor processor = hardware.getProcessor();

        CentralProcessor.ProcessorIdentifier processorIdentifier = processor.getProcessorIdentifier();

        System.out.println("Processor Vendor: " + processorIdentifier.getVendor());
        System.out.println("Processor Name: " + processorIdentifier.getName());
        System.out.println("Processor ID: " + processorIdentifier.getProcessorID());
        System.out.println("Identifier: " + processorIdentifier.getIdentifier());
        System.out.println("Microarchitecture: " + processorIdentifier.getMicroarchitecture());
        System.out.println("Frequency (Hz): " + processorIdentifier.getVendorFreq());
        System.out.println("Frequency (GHz): " + processorIdentifier.getVendorFreq() / 1000000000.0);
    }
}
The output is:
Processor Vendor: GenuineIntel
Processor Name: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Processor ID: BBBBFFFF000906EE
Identifier: Intel64 Family 6 Model 158 Stepping 10
Microarchitecture: Coffee Lake
Frequency (Hz): 2592000000
Frequency (GHz): 2.592

Example 3 Get Number of physical or logical CPUs

The CentralProcessor class also provides methods to get the number of packages, physical CPUs, logical CPUs as in the Java example below.

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;

public class ProcessorInfoExample3 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        CentralProcessor processor = hardware.getProcessor();

        System.out.println("Number of physical packages: " + processor.getPhysicalPackageCount());
        System.out.println("Number of physical CPUs: " + processor.getPhysicalProcessorCount());
        System.out.println("Number of logical CPUs: " + processor.getLogicalProcessorCount());
    }
}
The output is:
Number of physical packages: 1
Number of physical CPUs: 6
Number of logical CPUs: 12

Happy Coding 😊

Get Operating System 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

Tags: