Get MAC address for local host in Java
Description
The following code shows how to get MAC address for local host.
Example
//from w ww . ja va 2 s .com
import java.net.InetAddress;
import java.net.NetworkInterface;
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) ? "-" : "");
}
}
}
The code above generates the following result.