Java Mac Address Get getMacAddress()

Here you can find the source of getMacAddress()

Description

Reads the MAC address of the machine.

License

Apache License

Return

numeric value describing the mac address

Declaration

public static Long getMacAddress() 

Method Source Code

//package com.java2s;
/**//from  www .  j  av  a2 s.com
 * Copyright (C) 2012 - 101loops.com <dev@101loops.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;

public class Main {
    /**
     * Reads the MAC address of the machine. Since this only works on JDK 1.6+ it is wrapped in a
     * big try/catch block and returns null for older JDKs.
     *
     * @return numeric value describing the mac address
     */
    public static Long getMacAddress() {
        try {
            final InetAddress addr = getLocalHost();
            final NetworkInterface ni = NetworkInterface.getByInetAddress(addr);
            final byte[] mac = ni.getHardwareAddress();
            return ((long) mac[5] & 0xff) + (((long) mac[4] & 0xff) << 8) + (((long) mac[3] & 0xff) << 16)
                    + (((long) mac[2] & 0xff) << 24) + (((long) mac[1] & 0xff) << 32)
                    + (((long) mac[0] & 0xff) << 40);
        } catch (Throwable e) {
            return null;
        }
    }

    private static InetAddress getLocalHost() throws UnknownHostException {
        return InetAddress.getLocalHost();
    }
}

Related

  1. getMacAddress()
  2. getMACAddress()
  3. getMacAddress()
  4. getMacAddress()
  5. getMacAddress()
  6. getMacAddress()
  7. getMacAddress(String host)
  8. getMACAddress(String interfaceName)
  9. getMacAddress(String separator)