NetworkInterface.getHardwareAddress 这个方法就是获得机器的MAC地址的。
import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Formatter; import java.util.Locale; /** * 使用JAVA代码获得系统的MAC地址和IP地址。 * * @author 享受JAVA(enjoyjava.net) */ public class TestIpMac { public static void main(String[] args) throws Exception { InetAddress address = InetAddress.getLocalHost(); // 获得当前主机地址 NetworkInterface ni = NetworkInterface.getByInetAddress(address); // 获得网络接口 byte[] mac = ni.getHardwareAddress(); // 获得硬件的MAC地址 String sIP = address.getHostAddress(); // 获得IP地址 String sMAC = ""; Formatter formatter = new Formatter(); for (int i = 0; i < mac.length; i++) { // 组合成MAC的字符串 sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i], (i < mac.length - 1) ? "-" : "").toString(); } System.out.println("IP:" + sIP); System.out.println("MAC:" + sMAC); } }