Java Program to Get CPU Serial Number for Windows Machine

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:
- By running the command on Windows PowerShell.
- 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 classesimport java.io.BufferedReader;import java.io.File;import java.io.FileWriter;import java.io.InputStreamReader;Â
// Main class// WindowsCpuSerialNumberpublic 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.




