Java Program to Get CPU Serial Number for Windows Machine


CPU known as Central Processing Unit is the brain of the computer. It performs all the main operations in a computer. It is also called central processor or main processor. The CPU serial number is a unique identifier which is given to that particular CPU by the manufacturer to each individual central processor. Its main purpose is to track and identify the hardware for warranty claim. In this section, we are going to write a java program to get CPU serial number for windows machine.

CPU helps in executing the instructions and performing all the mathematical operations. CPU contains 3 components. The first component is storage unit. It is used to store information, data and results of the operations. It also helps to transfer data. This is the main memory also called as the random access memory. The second component is Control Unit. It is used to control all the operations of the computer and coordinates all the units of a computer. The third section is Arithmetic Logic Unit(ALU) which is used to perform all the arithmetic and logical operations of a computer.

Now, we will discuss various approaches in java to find the Serial Number for Windows Machine.

Approach 1: Using WMIC (Windows Management Instrumentation Command Line)

In this approach, we will use “wmic” commands in java code. “Wmic” is a command line tool for working with Windows management. “Wmic” stands for “Windows Management Instrumentation Command-Line”.

Algorithm

  • Create a process object with Runtime.getRuntime().exec() method by passing wmic cpu get ProcessorId as argument

  • Create a BufferedReader object using InputStreamReader as argument to read the output of process

  • Read the output using readLine() method and store it in the ‘line’ variable

  • while ‘line’ is not null and line is not equal to blank trim the spaces using trim() and print the output.

Example

In this example, the code retrieves the ProcessorId of the current system using the command line utility "wmic" (Windows Management Instrumentation Command).

A new Process object is created using exec() method of the Runtime class, passing the command "wmic cpu get ProcessorId" as a parameter. This command retrieves the ProcessorId of the current system's CPU.

In the next statement the process is being closed using getOutputStream() method and calling close() on the returned stream. This is because anything to be written on the process. Now a new BufferedReader object is created and initialized it with the input stream of the process using the getInputStream() method of the Process object. The BufferedReader object is used to read the output of the process. Finally, the code reads the output of the process line by line using the readLine() method of the BufferedReader object. If the line is not empty, the code trims the line and prints it to the console using System.out.println().

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
   public static void main(String[] args) {
      try {
         Process process = Runtime.getRuntime().exec("wmic cpu get ProcessorId");
         process.getOutputStream().close();
         BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
         String line;
         while ((line = reader.readLine()) != null) {
            if (!line.trim().equals("")){
               System.out.println(line.trim());
            }
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

ProcessorId
BFEBFBFF000906E9

Methods Used

Runtime.getRuntime().exec() − It is a method present in Runtime class in Java. This method is used to execute a command line command as s a process and which later can be interacted with. It takes a String as a parameter which is a command line command and output the process object of the command.

Runtime.getRuntime().exec(command) ; // executes the command as a process.

BufferedReader() − BufferedReader() method is a constructor of BufferedReader class in Java and returns us BufferedReader object which helps us to read a text from a file or network socket etc.This reads data as bytes and converts them to characters. It takes an object of FileReader or InputStreamReaader class as a parameter.

BufferedReader r = new BufferedReader(new InputStreamReader(System.in));

readLine() − It is a method present in Bufferreader class in Java and it reads line by line in a character stream and returns them as String This method is used to read lines from the BufferedReader object.

BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
PrintWriter writer = new PrintWriter(new FileWriter("output.txt"));

while ((line = reader.readLine()) != null) {  //reading line by line  on BufferReader Object 
   writer.println(line.toUpperCase()); // writing in output.txt file.
}

trim() − This method is used to remove spaces at start and end of a string. It is method present in String Class of Java.

tring str = "   Hello, World!   ";
System.out.println(str.trim()); // Output: "Hello, World!"

Approach 2: Using getenv()

In this approach, we will use the getenv() of the System class and the get the CPU Serial Number of a Windows Machine. This method is used to return the value of environment variables based on the parameter passed to this method. It accepts a String parameter and returns a String value. This method is present in System class of Java.

System.getenv("PATH");

Algorithm

  • Use getenv() method of System to get the CPU serial number by passing PROCESSOR_IDENTIFIER as argument

  • Print the number.

Example

In the above example, we used the Java System class and use the inbuilt getenv() method and pass the PROCESSOR_IDENTIFIER parameter then, the method returns a string which is the CPU serial number. We will now store the returned value in a string variable and print using the println() method.

import  java.util.*;
public class Main {
   public static void main(String[] args) {
      String serialNumber = System.getenv("PROCESSOR_IDENTIFIER");
      System.out.println("CPU serial number: " + serialNumber);
   }
}

Output

CPU serial number: null

Thus, in this article we have discussed different approaches to find the CPU Serial Number for Windows Machine using Java Programming Language.

Updated on: 16-Aug-2023

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements