Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 :

  1. First, we store the command that should have been run on terminal in a variable called command.
  2. Next, we initialize a Process with the call to the Runtime class of Java which is used to interact with the Java Runtime Environment.
  3. We pass the command as the parameter to the exec function whose primary function is to get the command executed.
  4. For the next step, we capture the Input Stream from the Process using InputStreamReader class of java I/O package.
  5. Then, the BufferedReader class is used to read the stream from the InputStreamReader.
  6. We store this in a variable.
  7. Next, we wait for the Process to terminate.
  8. We close the BufferedReader.
  9. In the catch block, we print the stack trace if an error has occurred and set the variable holding the Serial Number to null.
  10. 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 libraries
import 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 libraries
import 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


Last Updated : 04 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads