如何获得机器的mac地址

我想得到机器的MAC地址..但是下面写的代码只显示互联网连接到我的机器的MAC地址,否则它将返回null …我正在使用Windows 7

import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; class test { public static void main(String[] args) { InetAddress ip; try { ip = InetAddress.getLocalHost(); System.out.println("The mac Address of this machine is :" + ip.getHostAddress()); NetworkInterface network = NetworkInterface.getByInetAddress(ip); byte[] mac = network.getHardwareAddress(); System.out.print("The mac address is : "); 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()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (SocketException e) { e.printStackTrace(); } } } 

试试这个应该适用于Linux和Windows

 public static void main(String[] args) { String command = "/sbin/ifconfig"; String sOsName = System.getProperty("os.name"); if (sOsName.startsWith("Windows")) { command = "ipconfig"; } else { if ((sOsName.startsWith("Linux")) || (sOsName.startsWith("Mac")) || (sOsName.startsWith("HP-UX"))) { command = "/sbin/ifconfig"; } else { System.out.println("The current operating system '" + sOsName + "' is not supported."); } } Pattern p = Pattern .compile("([a-fA-F0-9]{1,2}(-|:)){5}[a-fA-F0-9]{1,2}"); try { Process pa = Runtime.getRuntime().exec(command); pa.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader( pa.getInputStream())); String line; Matcher m; while ((line = reader.readLine()) != null) { m = p.matcher(line); if (!m.find()) continue; line = m.group(); break; } System.out.println(line); } catch (Exception e) { e.printStackTrace(); } } 

好像你正试图通过IP地址获取MAC地址。 当您成功连接到Internet时, 存在IP地址。 否则,它将为null ,如您所述。

试试这个: NetworkInterface.getHardwareAddress()

如果您需要计算机上所有网络接口的MAC地址,请尝试以下方法: NetworkInterface.getNetworkInterfaces()


编辑 :再次审查代码后,我意识到你已经实施了我的建议。 但是, 如果您具有有效的IP,则只尝试获取MAC地址。 如果未连接到Internet,您将没有有效的IP。

  public static void main(String[] args) { NetworkInterface network; byte[] mac = network.getHardwareAddress(); System.out.print("The mac address is : "); 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()); } 

问题可能是由于当您的计算机未连接到Internet时,您的网卡没有分配的IP地址。 而您正试图通过IP地址查找网络接口。

我建议您枚举所有网络接口,然后选择您需要的接口:

 import java.net.*; import java.util.*; public class App { protected static String formatMac(byte[] mac) { if (mac == null) return "UNKNOWN"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } return sb.toString(); } public static void main(String[] args) throws Exception { for(Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) { NetworkInterface ni = e.nextElement(); System.out.println(ni.getName() + " - " + formatMac(ni.getHardwareAddress())); } } } 

这应该解决问题,并在没有任何Internet连接的情况下工作。