I have a chunk of code that returns the mac address of a PC very correctly, but that's only when there is an internet access, but I need it offline in a project I'm carrying out. If not possible, is there any other possible way of uniquely identifying a PC?
Asked
Active
Viewed 1,019 times
-2
-
Show us what you have tried, and tell us what part of it doesn't work. – antiduh Jul 23 '17 at 22:51
-
Refer to https://stackoverflow.com/a/6164182/1641451 - this will give you a list of NetworkInterfaces and you can then call getHardwareAddress on each NetworkInterface. – James Jul 23 '17 at 22:59
-
Thank you so much. – techiefrankie Jul 23 '17 at 23:02
-
Possible duplicate of [Get MAC address on local machine with Java](https://stackoverflow.com/questions/6164167/get-mac-address-on-local-machine-with-java) – Daniel Centore Jul 23 '17 at 23:59
1 Answers
2
You certainly use an indirection based on the IP address, in your code snippet. This may explain why you do not get anything when Internet network access is down.
Here is a code snippet that does not depend on the network connection status. It displays each MAC address of your PC. Note that a PC often has multiple MAC addresses. Each address will be displayed by this code snippet.
package com.stackoverflow;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetHWAddresses {
public static void main(String[] args) throws SocketException {
final Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
final byte [] mac = e.nextElement().getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++)
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
System.out.println(sb.toString());
}
}
}
}

Alexandre Fenyo
- 4,526
- 1
- 17
- 24