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


Motherboard Serial Number is an id assigned to the motherboard of a computer. It is used to generally used to track the computer system and also to identify when the system is lost or stolen. In this section, we will be discussing different approaches to find the Motherboard Serial Number for Windows and Linux machines using Java programming language.

Motherboard is the very crucial part if a computer. It is the backbone of the computer. All the components in a computer communicate through the motherboard. Motherboard helps in determining the factors like the amount of RAM we are going to get, Motherboard helps to configure the hardware and also helps in boot process. Serial number of a motherboard is the unique number that helps to identify the motherboard.

Let’s first discuss the important methods and syntaxes that we will use in the programs −

Runtime.getRuntime().exec() − It is a method present in Runtime class in Java. This method is used to execute a command line command as s a process and which later can be interacted with. It takes a String as a parameter which is a command line command and output the process object of the command.

Runtime.getRuntime().exec(command) ; // executes the command as a process.

BufferedReader() − BufferedReader() method is a constructor of BufferedReader class in Java and returns us 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 InputStreamReaader class as a parameter.

BufferedReader r = new BufferedReader(new InputStreamReader(System.in));

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 This method is used to read lines from the BufferedReader object.

BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
PrintWriter writer = new PrintWriter(new FileWriter("output.txt"));
while ((line = reader.readLine()) != null) {  //reading line by line  on BufferReader Object 
   writer.println(line.toUpperCase()); // writing in output.txt file.
}

trim() − This method is used to remove spaces at start and end of a string. It is method present in String Class of Java.

tring str = "   Hello, World!   ";
System.out.println(str.trim()); // Output: "Hello, World!"

Now we will be discussing in detail using examples to find Motherboard Serial Number for Windows and Linux Systems.

Approach for Windows Machine

In this approach, we will use “wmic” which is a command line tool for working with Windows management. “Wmic” stands for “Windows Management Instrumentation Command-Line”. It helps us to find Motherboard Serial Number for Windows Machine.

Algorithm: Using WMI (Windows Management Instrumentation)

  • Create a process object with Runtime.getRuntime().exec() method by passing wmic baseboard get serialnumber 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 the line variable

  • Trim the spaces using trim() and print the output until the readLine() is not null.

Example

In this example, the code retrieves the ProcessorId of the current system using the command line utility "wmic" (Windows Management Instrumentation Command).

A new Process object is created using exec() method of the Runtime class, passing the command "wmic baseboard get serialnumber" as a parameter. This command retrieves the ProcessorId of the current system's CPU.

In the next statement the process is being closed using getOutputStream() method and calling close() on the returned stream. This is because anything to be written on the process. Now a new BufferedReader object is created and initialized it with the input stream of the process using the getInputStream() method of the Process object.

The BufferedReader object is used to read the output of the process. Finally, the code reads the output of the process line by line using the readLine() method of the BufferedReader object. If the line is not empty, the code trims the line and prints it to the console using System.out.println().

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
   public static void main(String[] args) {
      try {
         Process process = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
         BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
         String line;
            while ((line = reader.readLine()) != null) {
            if (!line.trim().equals("SerialNumber")) {
               System.out.println("Motherboard Serial Number: " + line.trim());
            }
         }
         reader.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Motherboard Serial Number: MB-1234567890

Approach for Linux Machines

In this approach, we read a file from Linux systems to get the Mother Board Serial Number. The file which is to be read through code is present at /sys/class/dmi/id/board_serial. It is a file located in the sysfs filesystem on Linux systems that contains the serial number of the motherboard of the current system. sysfs is a virtual filesystem that provides a view of the system's hardware devices and their properties.

Algorithm

  • Create a BufferedReader object using /sys/class/dmi/id/board_serial as argument.

  • Read the output using readLine() method and store it in the line variable.

  • Print the line.

Example

In this approach, we will read a file which is present at ‘/sys/class/dmi/id/board_serial’. We read this using BufferReader object and pass the file location as a parameter to the BufferReader constructor. Then, we use the readLine() method to read the content and store in a variable from the BufferReader Object. After storing the value, we will print the output.

import java.io.BufferedReader;
import java.io.FileReader;

public class Main {
   public static void main(String[] args) {
      try {
         BufferedReader reader =newBufferedReader( newFileReader("/sys/class/dmi/id/board_serial"));
         String line = reader .readLine();
         System.out.println("Motherboard Serial Number: " + line);
         reader.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Motherboard Serial Number: MB-1234567890

Thus, we have discussed how to find the Motherboard Serial Number for Windows and Linux Machines using Java programming language in this article.

Updated on: 16-Aug-2023

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements