Java Mac Address Get getMacAddr()

Here you can find the source of getMacAddr()

Description

Retrive mac address for current machine

License

Open Source License

Declaration

public static String getMacAddr() 

Method Source Code

//package com.java2s;
/**//from  w  w  w.  ja v  a2 s  . co m
 * Project: com.dianping.avatar-core-1.0.0-SNAPSHOT
 * 
 * File Created at 2011-1-9
 * $Id$
 * 
 * Copyright 2010 dianping.com.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Dianping Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dianping.com.
 */

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    /**
     * Retrive mac address for current machine
     */
    public static String getMacAddr() {
        String macAddr = "";
        String str = "";
        try {
            NetworkInterface nic = NetworkInterface.getByName("eth0");
            if (nic != null) {
                byte[] buf = nic.getHardwareAddress();
                if (buf != null) {
                    for (int i = 0; i < buf.length; i++) {
                        str = str + byteHEX(buf[i]);
                    }
                }
                macAddr = str.toUpperCase();
            } else {
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                NetworkInterface networkInterface = null;
                while (networkInterfaces.hasMoreElements()) {
                    networkInterface = networkInterfaces.nextElement();
                    Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                    while (inetAddresses.hasMoreElements()) {
                        InetAddress inetAddress = inetAddresses.nextElement();
                        if (!inetAddress.isLoopbackAddress() && !Inet6Address.class.isInstance(inetAddress)) {
                            break;
                        }
                    }
                }
                if (networkInterface != null) {
                    byte[] buf = networkInterface.getHardwareAddress();
                    if (buf != null) {
                        for (int i = 0; i < buf.length; i++) {
                            str = str + byteHEX(buf[i]);
                        }
                    }
                }
                macAddr = str.toUpperCase();
            }
        } catch (SocketException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return macAddr;
    }

    public static String byteHEX(byte ib) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] ob = new char[2];
        ob[0] = Digit[(ib >>> 4) & 0X0F];
        ob[1] = Digit[ib & 0X0F];
        String s = new String(ob);
        return s;
    }
}

Related

  1. getMac()
  2. getMacAddress()
  3. getMacAddress()
  4. getMacAddress()
  5. getMacAddress()