Previously for obtaining a MAC address we need to use a native code as a solution. In JDK 1.6 a new method is added in the java.net.NetworkInterface class, this method isgetHardwareAddress().
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class MacAddress {
public static void main(String[] args) {
try {
//InetAddress address = InetAddress.getLocalHost();
InetAddress address = InetAddress.getByName("192.168.46.53");
/*
* Get NetworkInterface for the current host and then read the
* hardware address.
*/
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
/*
* Extract each array of mac address and convert it to hexa with the
* following format 08-00-27-DC-4A-9E.
*/
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
} else {
System.out.println("Address doesn't exist or is not accessible.");
}
} else {
System.out.println("Network Interface for the specified address is not found.");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
Hello! Is it alright that I go a bit off topic? I am trying to view your website on my iPod Touch but it doesn’t display properly, do you have any suggestions? You can always email me at annen@gmail.com Thanks! Raelene
I would like to see something more about this. You captured my attention and hope I will see more from you here.
Yo! Is it OK if I go a bit off topic? I’m trying to read your blog on my iPod Touch but it doesn’t display properly, any suggestions? You can always email me at achuca@gmail.com Thank you for the help I hope! Ervin
Awesome post! I really appreciate the useful information that you shared here. It is really beneficial read for me. Thanks for sharing
Nice post, I gonna bookmark this page. thanks for info
Гений, прикованный к чиновничьему столу, должен умереть или сойти с ума, точно так же, как человек с могучим телосложением при сидячей жизни и скромном поведении умирает от апоплексического удара.