Open In App

Java Program to Get CPU Serial Number for Windows Machine

Last Updated : 04 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

CPU Serial Number (or Processor Serial Number) is a software-readable unique serial number that Intel has stamped into its Pentium 3 microprocessor. Intel offers this as a feature that can be optionally used to provide certain network management and e-commerce benefits. Basically, it lets a program identify individual PCs.

We can get the CPU Serial number of a Windows machine in 2 ways:

  1. By running the command on Windows PowerShell.
  2. Using FileWriter class in java

Way 1: Running PowerShell command

It is a similar way to what we say running commands on terminals on Mac. For windows, it’s CMD for which we do have a one-liner pred-defined command below. You simply need to write it as it is or copy the same from here which is given below as follows:

Syntax: 

WMIC BIOS GET SERIALNUMBER 

This pop-up window will appear letting us revealing the CPU serial number for the windows machine. 

Way 2: Using FileWriter class 

Java FileWriter class of java.io package is used to write data in character form to file.

  • This class inherits from OutputStreamWriter class which in turn inherits from the Writer class.
  • The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
  • FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.
  • FileWriter creates the output file if it is not present already.

Example

Java




// Java Program to get CPU Serial Number of Windows Machine
// using FileWriter class
 
// Importing required classes
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
 
// Main class
// WindowsCpuSerialNumber
public class GFG {
 
    // Method 1
    // To get CPU serial number
    private static String getWindowsCPU_SerialNumber()
    {
 
        // Initially declaring and initializing an empty
        // string
        String result = "";
 
        // Try block to check for exceptions
        try {
 
            // Creating an object of File class
            File file
                = File.createTempFile("realhowto", ".vbs");
 
            // Deleting file while exiting
            file.deleteOnExit();
 
            // Creating an object of FileWriter class to
            // write on
            FileWriter fw = new java.io.FileWriter(file);
 
            // Remember the command
            String vbs1
                = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                  + "Set colItems = objWMIService.ExecQuery _ \n"
                  + "   (\"Select * from Win32_Processor\") \n"
                  + "For Each objItem in colItems \n"
                  + "    Wscript.Echo objItem.ProcessorId \n"
                  + "    exit for  ' do the first cpu only! \n"
                  + "Next \n";
 
            // Writing on file
            fw.write(vbs1);
 
            // Closing all file connections to
            // release memory spaces
            fw.close();
 
            Process p = Runtime.getRuntime().exec(
                "cscript //NoLogo " + file.getPath());
 
            BufferedReader input = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
 
            String line;
 
            while ((line = input.readLine()) != null) {
                result += line;
            }
 
            input.close();
        }
 
        // Catch block to handle the exceptions
        catch (Exception E) {
 
            // Print the exception along with the message
            System.err.println("Windows CPU Exp : "
                               + E.getMessage());
        }
 
        return result.trim();
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        String cpuSerialNumber
            = getWindowsCPU_SerialNumber();
 
        // Calling the method1 to retrieve CPU serial number
        // and printing the same
        System.out.println(
            "CPU Serial Number of my Windows Machine: "
            + cpuSerialNumber);
    }
}


Output:

Below is the hard-coded output when run on a Windows machine with the help of FileWriter class.

Note: In addition to this if these programs are windows specific and should not be run on other operating systems. Approach 1 will not work and for approach 2 that is the above approach been laid via FileWriter class if, on Mac the output is as follows. It is because ‘cscript’ cant be run on the terminal.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads