Java NetworkInterface get Mac address

Description

Java NetworkInterface get Mac address

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

public class Main {
   public static void main(String args[]) throws Exception {
      Enumeration<NetworkInterface> e =  NetworkInterface.getNetworkInterfaces();
      //from   ww  w. ja va2 s . co m
      for(;e.hasMoreElements();) {
         NetworkInterface intf = e.nextElement();
         System.out.println("\nName : " + intf.getName());
         System.out.println("Display name : " + intf.getDisplayName());
         System.out.println("Up : " + intf.isUp());
         System.out.println("Loopback : " + intf.isLoopback());
         System.out.println("PointToPoint : " + intf.isPointToPoint());
         System.out.println("Supports multicast :" + intf.supportsMulticast());
         System.out.println("Virtual : " + intf.isVirtual());

         byte[] mac1 = intf.getHardwareAddress();
         if (mac1 != null) {
            System.out.print("Hardware Address : ");
            for (int k = 0; k < mac1.length; k++)
               System.out.format("%02X%s", mac1[k], (k < mac1.length - 1) ? "-" : "");
            System.out.println();
         }
         System.out.println("MTU :" + intf.getMTU());
         
      }
   }
}



PreviousNext

Related