i have the following code to extract MAC Address for my Ubuntu device & i am putting the MAC Address in database. This code works fine In windows PC where as the same doesn't work in Ubuntu Device . The database field gets empty. My Application is a web application. I am taking the war file & putting that in the tomcat server which is there in my Ubuntu device.
public static String getMACAddress()
{
StringBuffer strMac = new StringBuffer();
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) ? "-" : "");
strMac.append(String.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 (Exception e) {
}
return strMac.toString();
}
I have also referred this(Java - Getting MAC address of Linux system) link.
My Ubuntu device uses both LAN & Wifi connection .
Hope my question is clear. Please help ... Thanks