Java Program to Get System Motherboard Serial Number for Windows and Linux Machine
Last Updated :
04 Jan, 2021
The motherboard is the foundation on top of which the entire architecture of the CPU is based upon. It is the primarily printed Circuit Board (PCB) that is responsible for connecting all the major components such as hard drive, CPU, external ports and so on. A good and knowledgeable computer user must be aware of the Manufacturer, Version, and Serial Number of their board. This article primarily revolves around writing Java codes to extract the Serial Numbers of the motherboard for the respective machine of the user. The Serial Number is unique for every motherboard and hence, every user will get different outputs on running the same program. Below, we have provided the codes to find out the Serial Numbers for motherboards for both Windows and Linux machines.
A. Linux Machine :
The primary concept in retrieving the Serial Number of the Motherboard is to run commands in the terminal using the Java code and storing the retrieved Serial Number as a String which is then printed on the screen.
Algorithm :
- First, we store the command that should have been run on terminal in a variable called command.
- Next, we initialize a Process with the call to the Runtime class of Java which is used to interact with the Java Runtime Environment.
- We pass the command as the parameter to the exec function whose primary function is to get the command executed.
- For the next step, we capture the Input Stream from the Process using InputStreamReader class of java I/O package.
- Then, the BufferedReader class is used to read the stream from the InputStreamReader.
- We store this in a variable.
- Next, we wait for the Process to terminate.
- We close the BufferedReader.
- In the catch block, we print the stack trace if an error has occurred and set the variable holding the Serial Number to null.
- Finally, we return this variable and print the output using the driver code.
Command Used :
sudo dmidecode -s baseboard-serial-number
Below is the implementation of the problem statement:
Java
import java.io.*;
class GFG {
static String getLinuxMotherBoardSerialNumber()
{
String command
= "sudo dmidecode -s baseboard-serial-number" ;
String serialNumber = null ;
try {
Process SerialNumberProcess
= Runtime.getRuntime().exec(command);
InputStreamReader ISR = new InputStreamReader(
SerialNumberProcess.getInputStream());
BufferedReader br = new BufferedReader(ISR);
serialNumber = br.readLine().trim();
SerialNumberProcess.waitFor();
br.close();
}
catch (Exception e) {
e.printStackTrace();
serialNumber = null ;
}
return serialNumber;
}
public static void main(String[] args)
{
System.out.println(
getLinuxMotherBoardSerialNumber());
}
}
|
Output :
PGPPP018J940BP
B. Windows Machine :
The only difference in the code that is used to retrieve the Serial Number of Motherboard on Windows machine from that used to retrieve it on the Linux machine is of the command used. The remaining algorithm as well as the code remains the same.
Command Used:
wmic baseboard get serialnumber
Below is the implementation of the problem statement:
Java
import java.io.*;
class GFG {
static String getWindowsMotherBoardSerialNumber()
{
String command = "wmic baseboard get serialnumber" ;
String serialNumber = null ;
try {
Process SerialNumberProcess
= Runtime.getRuntime().exec(command);
InputStreamReader ISR = new InputStreamReader(
SerialNumberProcess.getInputStream());
BufferedReader br = new BufferedReader(ISR);
serialNumber = br.readLine().trim();
SerialNumberProcess.waitFor();
br.close();
}
catch (Exception e) {
e.printStackTrace();
serialNumber = null ;
}
return serialNumber;
}
public static void main(String[] args)
{
System.out.println(
getWindowsMotherBoardSerialNumber());
}
}
|
Output:
PGPPP018J940BP
Share your thoughts in the comments
Please Login to comment...