Java Program to Get CPU Serial Number for Windows Machine
Last Updated :
04 Nov, 2022
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
public class GFG {
private static String getWindowsCPU_SerialNumber()
{
String result = "" ;
try {
File file
= File.createTempFile( "realhowto" , ".vbs" );
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
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" ;
fw.write(vbs1);
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 (Exception E) {
System.err.println( "Windows CPU Exp : "
+ E.getMessage());
}
return result.trim();
}
public static void main(String[] args)
{
String cpuSerialNumber
= getWindowsCPU_SerialNumber();
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.
Share your thoughts in the comments
Please Login to comment...