Get MAC address of a host: : NetworkInterface « Network Protocol « Java

Get MAC address of a host:



public class Main {
  public static void main(String[] args) throws Exception {
    InetAddress address = InetAddress.getLocalHost();

    NetworkInterface ni = NetworkInterface.getByInetAddress(address);
    byte[] mac = ni.getHardwareAddress();

    for (int i = 0; i < mac.length; i++) {
      System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");


Related examples in the same category

1.Programmatic Access to Network Parameters
2.An updated version of the ListNets program that uses the NetworkInterface enhancements
3.Report By Name
4.List Network Interfaces
5.Extract network card address with
6.Class for determining a host's IP address.