Get MAC address on local machine with Java

2019-01-03 00:02发布

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?

8条回答
贼婆χ
2楼-- · 2019-01-03 00:34

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-03 00:38

As for the computer being offline, it usually doesn't have an IP assigned, because DHCP is widely used...

And for the question in the title: NetworkInterface.getHardwareAddress()

查看更多
登录 后发表回答