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?
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?
Try this:
output:
@Jesus Flores, still not working for me, this code:
is throwing exception. We have to check for null. Final solution in your case that is working for me:
I am working always over wi-fi.
Another way is to use an OS command 'getmac' through native code execution.
Cleaned up code from here:
With all the possible solutions that i've found here and another replies, then i will contribute with my solution. You need to specify a parameter with a String containing "ip" or "mac" depending on what you need to check. If the computer has no interface, then it will return an String containing null, otherwise will return a String containing what you asked for (the ip address or the mac).
How to use it:
Result (if the computer has a network card):
Result (if the computer doesn't have a network card):
Here's the code:
UPDATED 02/05/2017: Thanks to @mateuscb on the post How to Determine Internet Network Interface in Java that unfortunately didn't get any upvote on that post before, but he contributed to this update.
The method has been improved to skip virtual machine network cards (most popular VM software)