How do I get MAC address of a host?

Category: java.net, viewed: 2491 time(s).

Previously for obtaining a MAC address we need to use a native code as a solution. In JDK 1.6 a new method is added in the java.net.NetworkInterface class, this method is getHardwareAddress().

 
package org.kodejava.example.net;
 
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
 
public class MacAddress {
	
    public static void main(String[] args) {
        try {		
            InetAddress address = InetAddress.getLocalHost();
			
            /*
             * Get NetworkInterface for the current host and then read the 
             * hardware address.
             */
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            byte[] mac = ni.getHardwareAddress();
			
            /*
             * Extract each array of mac address and convert it to hexa with the 
             * following format 08-00-27-DC-4A-9E.
             */
            for (int i = 0; i < mac.length; i++) {			
                System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();	
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }	
}
 
 
Bookmark this example!  

Most Viewed Examples

Google

100 Top & Latest


eXTReMe Tracker
visitor stats