Get MAC address on local machine with Java

Jevivre xavie picture Jevivre xavie · May 28, 2011 · Viewed 71k times · Source

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?

Answer

phihag picture phihag · May 28, 2011

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().