Getting MAC address of any system is very simple. But how can we get the MAC address programmatically using Java ?
Let see, how it is possible in Java ?
try{
String IP =InetAddress.getLocalHost().getHostAddress();//get the system ip address
//System.out.print(IP);
String hostname=InetAddress.getLocalHost().getHostName();//get the system name
//get mac addrress
InetAddress inet=InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(inet);
byte[] mac = network.getHardwareAddress();
//System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
String mac1=sb.toString();// Get the Mac Address in a string
}
catch(Exception e)
{
}
No comments:
Post a Comment