Java Mac Address Get getMacAddress()

Here you can find the source of getMacAddress()

Description

Returns the computer MAC address in 5C-26-0A-88-4E-DA format.

License

Open Source License

Return

the name or "00" if the MAC address cannot be found

Declaration


public static String getMacAddress() 

Method Source Code


//package com.java2s;
/*/*from  ww  w . j  a  v a2 s . co m*/
 * This file is part of AceQL. 
 * AceQL: Remote JDBC access over HTTP.                                     
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * AceQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * AceQL is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */

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

public class Main {
    /**
     * Returns the computer MAC address in 5C-26-0A-88-4E-DA format.
     * 
     * @return the name or <b>"00"</b> if the MAC address cannot be found
     */

    public static String getMacAddress() {

        String macAddress = null;

        try {
            InetAddress ip = InetAddress.getLocalHost();

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            if (network == null) {
                macAddress = "00";
                return macAddress;
            }

            byte[] mac = network.getHardwareAddress();

            if (mac == null) {
                macAddress = "00";
                return macAddress;
            }

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }

            macAddress = sb.toString();
            return macAddress;
        } catch (Exception e) {
            macAddress = "00";
            return macAddress;
        }

    }
}

Related

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