Search code examples
javasocketsmac-address

Get MAC address on local machine with Java


I can use

ip = InetAddress.getLocalHost();
NetworkInterface.getByInetAddress(ip);

to obtain the mac address, but if I use this code in an offline machine it doesn't work.

So, How can I get the Mac address?


Solution

  • With Java 6+, you can use NetworkInterface.getHardwareAddress.

    Bear in mind that a computer can have no network cards, especially if it's embedded or virtual. It can also have more than one. You can get a list of all network cards with NetworkInterface.getNetworkInterfaces().