Java macaddress – Java Program to Get MAC Address

Java macaddress: In this article we will see how you can find the system MAC address of Windows by using Java programming language.

Java Program to Get MAC Address

What is MAC address?

Java get mac address: MAC (Media Control Address) address refers to a unique identification number which is assigned to the NIC (Network Interface Controller) to identify a system uniquely over a network. This address is assigned to the system during manufacture process. It is also called as Device address or Physical address.

A single system can have many Network Interface Cards hence it can have many MAC addresses. You can see all MAC addresses of your system by using ipconfig /all command in command prompt.

Explanation:

In Java we have java.net.NetworkInterface class which encapsulates data for network interfaces and java.net.InetAddress class which encapsulates both, the numerical IP address and the domain name for that address. By using these classes of java.net package we will see how we can get the MAC address of system.

Let’s see the program to understand it more clearly.

Approach:

  • Create a variable of type InetAddress and by Using getLocalHost() method get the IP address & assign it to the variable.
  • Create a variable of type NetworkInterface and by Using getByInetAddress() method search for the network interface that has the specified IP address bound to it, we have passed the IP as parameter.
  • Then By using getHardwareAddress() method get MAC address and assign it to a byte array.
  • Convert byte array to String and by using for loop.
  • Then print the final MAC address.

Program:

import java.net.*;
public class GetMACAddress 
{
        //driver method
        public static void main(String[] args) 
          {
            try 
            {
              //Created a variable of type InetAddress
              //By Using getLocalHost() get the IP address & assign it to the variable
              InetAddress ip = InetAddress.getLocalHost();
              System.out.println("IP address : " + ip.getHostAddress());
              
              //Created a variable of type  NetworkInterface
              //By Using getByInetAddress() method search for the network interface 
              //that has the specified IP address bound to it, we have passed the IP as parameter.
              NetworkInterface network = NetworkInterface.getByInetAddress(ip);
              //By using getHardwareAddress() method get MAC address
              byte[] mac = network.getHardwareAddress();
              System.out.print("MAC address : ");
              
              //converting the byte array into a string
              StringBuilder s = new StringBuilder();
              for (int i = 0; i < mac.length; i++) 
              {
                s.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
              }
              //final string which is our MAC address
              System.out.println(s.toString());
            } 
            catch (Exception e) 
            {
              e.printStackTrace();
            }
          }
}

Output:

IP address : 192.168.0.103
MAC address : C0-B5-D7-C6-58-17

When system has more than one MAC addresses – Get all the MAC addresses:

Program:

import java.net.*;
import java.util.Enumeration;
public class GetMACAddress 
{
        //driver method
        public static void main(String[] args) 
          {
             try 
             {      
               // created an Enumeration of type NetworkInterface 
               //Store the values returned by getNetworkInterfaces()
               Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
                    
               // for every network in the networks Enumeration
               while (networks.hasMoreElements()) 
               {
                 NetworkInterface network = networks.nextElement();
                 //get the MAC address and store it in byate array
                 byte[] mac = network.getHardwareAddress();
                 //converting byte array to string
                 if (mac != null) 
                 {
                   System.out.print("Current MAC address : ");
                   StringBuilder sb = new StringBuilder();
                   for (int i = 0; i < mac.length; i++) 
                   {
                                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                   }
                   //print final MAC address
                   System.out.println(sb.toString());
                  }
                }
               }
                catch (SocketException e) 
                {
                    e.printStackTrace();
                }
          }
}

Output:

Current MAC address : C0-B5-D7-C6-58-17
Current MAC address : E4-54-E8-20-2D-5E
Current MAC address : C0-B5-D7-C6-58-18
Current MAC address : D2-B5-D7-C6-58-17
Current MAC address : C2-B5-D7-C6-58-17

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.