
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get CPU Serial Number for Windows Machine in Java
The serial number of a CPU is a unique identifier which is given to a particular CPU by the manufacturer to each of them. Its main purpose is to track and identify the hardware for warranty claim. In this article, we are going to write a java program to get CPU serial number for windows machine. Central Processing Unit, in short CPU, is the brain of the computer.
Using WMIC Command
In this approach, we will use wmic command in java code. The wmic is a command line tool for working with Windows management. It stands for Windows Management Instrumentation Command-Line.
Follow the steps given below ?
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 a String.
Print the output.
NOTE: The wmic command might not work on all Windows versions. Additionally, the output may depend on the manufacturer and model of the CPU.
Example
Here is the practical implementation of the above mentioned approach ?
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(); } } }
This code will produce the following result on running in your local machine ?
ProcessorId BFEBFBFF000906E9
Methods Used
Runtime.getRuntime().exec() ? It is a method present in Runtime class in Java. It takes a String as a parameter which is a CLI command and display the process object of the command.
BufferedReader() ? It is a constructor of BufferedReader class in Java. It returns 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 InputStreamReader class as a parameter.
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.
trim() ? This method is used to remove spaces at start and end of a string. It is method present in String Class of Java.
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.
Syntax
System.getenv("PATH");
Example
Let's see the demonstration ?
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); } }
Run this Java code on your local machine to get the CPU serial number.
CPU serial number: Intel64 Family 6 Model 140 Stepping 1, GenuineIntel