Java Program to Get System MAC Address of Windows and Linux Machine


Computers can connect to a network and communicate with other devices using a hardware or software component known as Network Interface Controller(NIC). NIC helps in creating a layer which helps in transmitting and receiving data between two devices.MAC address known as Media Access Control address is a unique identifier of NIC. It is of 48 bits of hexadecimal digits. It is used by the data link layer to control access, ensure data integrity, and identify network devices. In this section, we will be learning about how to find a MAC address of a Windows and Linux machine using Java.

A device MAC address does not change. MAC address is used by network devices like routers and switches. These devices like routers and switches generally used to forward data packets between different devices on a network. When a device send data packets to another device on the same network, the data packet includes the MAC address of the intended recipient in the data packet header. The network devices then use the MAC address which they received from packet headers and deliver the packet to the intended recipient.

Now, we will look into detail about the different methods used to find the MAC Address of Linux and Windows machines. Java Provides InetAddress class and NetworkInterface class to perform the operations on MAC Address.

Now, we will look into different code implementations for finding the MAC address of Windows and Linux Machines.

Finding MAC Address of Windows Machine

We will implement java code to find the MAC Address of Windows Machine. The algorithm to find the MAC Address is as below.

Algorithm

  • Get instance of InetAddress using InetAddress.getLocalHost().

  • Get NetworkInterface instance by using NetworkInterface.getByInetAddress() by passing InetAddress object. Get MAC address using getHardwareAddress() on network interface instance and store in a byte array.

  • Declare a string builder and loop through the byte array.

  • Append the bytes by converting them to hexadecimal digits using format() and print it.

Example

In this example, we created a InteAddress instance initially using ‘InetAddress.getLocalHost()’ and it is passed to NetworkInterface constructor which helps us to create a NetworkInterface Object. Then we call the inbuilt ‘getHardwareAddress()’ method on the NetworknIterface object to get the mac address in byte array format. After getting the byte array of mac address, we use the string builder object and loop over the mac array and convert the bytes into hexadecimal format and print the output.

import java.net.*;
import java.util.*;
public class Main {
   public static void main(String args[]) throws Exception {
   InetAddress address = InetAddress.getLocalHost();
   NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address);
   byte[] mac = networkInterface.getHardwareAddress();
   System.out.print("MAC address  : ");
   StringBuilder stringBuilder = new StringBuilder();
   for (int i = 0; i < mac.length; i++) {
      stringBuilder.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
      }
      System.out.println(stringBuilder.toString());
   }
}

Output

MAC address of this system is : 00-15-F2-5D-8A-80

Finding MAC Address of Linux Machine

We will implement java code to find the MAC Address of Linux Machine. The steps to find the MAC Address is as below.

Steps

  • Import all the required Java libraries.

  • Get all network interfaces using NetworkInterface.getNetworkInterfaces()

  • For each network get the MAC address using network.getHardwareAddress()

  • If MAC address is not null, Declare a string builder and loop through the MAC address.

  • Append the bytes by converting them to hexadecimal digits using format() and print it

Example

In this example, we use the function ‘NetworkInterface.getNetworkInterfaces()’ and get the Enumeration of NetworkInterfaces. We then iterate over this enumeration until there is an NetworkInterface present in enumeration, We get the MAC address of each NetworkInterface by using the ‘getHardwareAddress()’ function which generally return a byte array of mac address and this array is converted to hexadecimal values by using StringBuilder Object and format() method and append all the content to StringBuilder object and print the value by converting the StringBuilder object to toString() method. Thus, we get the MAC address of the Linux machines using Java code.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class Main{
   public static void main(String[] args) {
      try {
         Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
         while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            byte[] mac = networkInterface.getHardwareAddress();
            if (mac != null) {
               System.out.print("MAC address : ");
               StringBuilder stringBuilder = new StringBuilder();
               for (int i = 0; i < mac.length; i++) {
                  stringBuilder.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
               }
               System.out.println(stringBuilder.toString());
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

MAC address : 54-E1-AD-3F-2E-63
MAC address : 0A-B1-CD-2F-6A-83
MAC address : 12-34-56-78-9A-BC

Thus, in this article, we have learned how to get the system MAC address of Windows and Linux Machine using Java.

Updated on: 16-Aug-2023

918 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements