Java gets the MAC address of the MAC OS network card
package com.macro.mall.portal.util;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class mac {
public static void main(String[] args) {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
byte[] macAddress = networkInterface.getHardwareAddress();
if (macAddress != null) {
System.out.print("MAC address for " + networkInterface.getName() + ": ");
for (int i = 0; i < macAddress.length; i++) {
System.out.format("%02X%s", macAddress[i], (i < macAddress.length - 1) ? "-" : "");
}
System.out.println();
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
Test results: The data obtained by direct request + the data obtained by changing the network request are the same. After shutdown and restart: MAC address for llw0, MAC address for awdl0 will change. If you want a computer to always obtain the same data, you can Add a judgment, and then output
if ("en0".equals(nic.getName())) {
}