Site icon codippa

How to get all MAC addresses of a system from java program

What is MAC address

MAC(Media Access Control) address of a device is a unique identity assigned to it. It is used to identify a computer over a network. Similar to IP address, MAC address of a device is also unique. A MAC address is assigned during the manufacture process of hardware and remains constant throughout its life. It is also referred to as device address or physical address.
MAC address is only assigned to Network Interface Controllers. These are hardware devices that connect to a network. Since these devices connect a computer to a network, they act as an interface between the two, hence called Network Interface Controllers or Network adapters.
If you want to uniquely identify a computer, then a MAC address would help you out. MAC address is also called Physical address.

A Computer can have many MAC Addresses

As explained above, a Network Interface Controller is the point at which a computer connects to a network and MAC address is the unique identification number assigned to it. A single computer can have many Network Interface Controllers or cards which means that it can have many MAC addresses
In order to see all the MAC addresses of your computer, open command prompt on Windows and type ipconfig /all (ifconfig on Macintosh OS) command. This will list out all the physical addresses of the computer with an output as shown below.

Physical address refers to MAC address only. The above command will list out other details also which have been omitted.
It is also possible to get all the MAC addresses of a system from java program. This post will demonstrate how.

Get All MAC addresses from java

Java provides a class to interact and retrieve information about Network Interfaces. This class is java.net.NetworkInterface. It has a method getNetworkInterfaces which represents all network interfaces(or network adapters) of the current system. Each network adapter can then be queried for its physical or MAC address as shown in the program below. Also, every network adapter has a name associated with it which can be retrieved using its getName method.

 import java.net.NetworkInterface;
 import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.util.Enumeration;

 public class MacAddressCalculator {
   public static void main(String[] args) {
      try {
         // get all network interfaces of the current system
         Enumeration networkInterface = NetworkInterface.getNetworkInterfaces();
         // iterate over all interfaces
         while (networkInterface.hasMoreElements()) {
            // get an interface
            NetworkInterface network = networkInterface.nextElement();
            // get its hardware or mac address
            byte[] macAddressBytes = network.getHardwareAddress();
            if (macAddressBytes != null) {
               System.out.print("MAC address of interface \""+network.getName()+"\" is : ");
               // initialize a string builder to hold mac address
               StringBuilder macAddressStr = new StringBuilder();
               // iterate over the bytes of mac address  
               for (int i = 0; i < macAddressBytes.length; i++) {
                  // convert byte to string in hexadecimal form
                  macAddressStr.append(String.format("%02X", macAddressBytes[i]));
                  // check if there are more bytes, then add a "-" to make it more readable
                  if(i < macAddressBytes.length - 1) {
                     macAddressStr.append("-");
                  }
               }
             System.out.println(macAddressStr.toString());
         }
      } catch (UnknownHostException e) {
         e.printStackTrace();
      } catch (SocketException e) {
         e.printStackTrace();
      }
    }
 }

Each network interface represents a hardware device of the system. getNetworkInterface method of class java.net.NetworkInterface returns the hardware or MAC address of the interface in the form of a byte array. This byte array is then iterated and each byte is converted to its hexadecimal form using format method of java.lang.String class. Following line converts a byte to hexadecimal of 2 digits

String.format("%02X", macAddressBytes[i])

Format %02X means convert into hexadecimal of 2 digits.

Output

MAC address of interface "wlan1" is 71-07-15-47-DB-FG
MAC address of interface "eth2" is 6B-07-15-47-DB-BT
MAC address of interface "wlan4" is 7A-07-15-47-DB-AA
MAC address of interface "wlan5" is 68-07-15-47-DB-AA
MAC address of interface "eth4" is 7A-15-81-2C-ED-45
MAC address of interface "eth5" is 00-15-5D-E6-2D-10
MAC address of interface "eth9" is 09-FF-99-78-B9-5D
MAC address of interface "eth10" is C9-6B-76-5D-59-11

There can be many network interfaces of a single system but each network interface can have only one MAC address.

It is also possible to get a network interface by its name and then retrieve its hardware address. A network interface can be retrieved by using static method getByName of java.net.NetworkInterface class passing it the name of network interface as shown below.

// get network interface by its name
NetworkInterface networkInterface = NetworkInterface.getByName("wlan1");
// get its mac address
byte[] macAddressBytes = networkInterface.getHardwareAddress();

Hope this post will help you to gain some additional information. Keep visiting for more!!!

Exit mobile version