Java Program to Get System Motherboard Serial Number for Windows and Linux Machine - GeeksforGeeks

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.
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
// Java code to get the system// motherboard serial number on linux  // importing the librariesimport java.io.*;  class GFG {    static String getLinuxMotherBoardSerialNumber()    {          // command to be executed on the terminal        String command            = "sudo dmidecode -s baseboard-serial-number";          // variable to store the Serial Number        String serialNumber = null;          // try block        try {              // declaring the process to run the command            Process SerialNumberProcess                = Runtime.getRuntime().exec(command);              // getting the input stream using            // InputStreamReader using Serial Number Process            InputStreamReader ISR = new InputStreamReader(                SerialNumberProcess.getInputStream());              // declaring the Buffered Reader            BufferedReader br = new BufferedReader(ISR);              // reading the serial number using            // Buffered Reader            serialNumber = br.readLine().trim();              // waiting for the system to return            // the serial number            SerialNumberProcess.waitFor();              // closing the Buffered Reader            br.close();        }          // catch block        catch (Exception e) {              // printing the exception            e.printStackTrace();              // giving the serial number the value null            serialNumber = null;        }          // returning the serial number        return serialNumber;    }      // Driver Code    public static void main(String[] args)    {          // printing and calling the method which        // returns the Serial Number        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
// Java code to get the system// motherboard serial number on Windows  // importing the librariesimport java.io.*;  class GFG {    static String getWindowsMotherBoardSerialNumber()    {          // command to be executed on the terminal        String command = "wmic baseboard get serialnumber";          // variable to store the Serial Number        String serialNumber = null;          // try block        try {              // declaring the process to run the command            Process SerialNumberProcess                = Runtime.getRuntime().exec(command);              // getting the input stream using            // InputStreamReader using Serial Number Process            InputStreamReader ISR = new InputStreamReader(                SerialNumberProcess.getInputStream());              // declaring the Buffered Reader            BufferedReader br = new BufferedReader(ISR);              // reading the serial number using            // Buffered Reader            serialNumber = br.readLine().trim();              // waiting for the system to return            // the serial number            SerialNumberProcess.waitFor();              // closing the Buffered Reader            br.close();        }          // catch block        catch (Exception e) {              // printing the exception            e.printStackTrace();              // giving the serial number the value null            serialNumber = null;        }          // returning the serial number        return serialNumber;    }      // Driver Code    public static void main(String[] args)    {          // printing and calling the method which        // returns the Serial Number        System.out.println(            getWindowsMotherBoardSerialNumber());    }}
Output:
PGPPP018J940BP
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class