2012-08-09 95 views
3

我想拿到机器的MAC address..but低于当Internet连接到其他的我的机器编写的代码仅显示MAC地址,将返回空...我使用Windows 7如何让机器的mac地址

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(); 
     } 
    } 
} 
+4

你需要开始缩进你的代码工作。由于缺乏缩进,现在很难阅读! – ThiefMaster 2012-08-09 13:39:18

+0

如果你的盒子没有连接到互联网会发生什么?输出是什么? – home 2012-08-09 13:40:55

+0

[获取本地计算机上的MAC地址与Java]的可能的重复(http://stackoverflow.com/questions/6164167/get-mac-address-on-local-machine-with-java) – 2012-08-09 13:45:27

回答

5

好像你正试图通过IP地址获取MAC地址。一个IP地址存在只有当你连接成功到互联网。否则,如您所述,它将是null

试试这个:NetworkInterface.getHardwareAddress()

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


编辑:再次检查代码后,我才知道你有我的建议执行。但是,如果您拥有有效的IP,则您只会尝试获取MAC地址。如果没有连接到互联网,您将不会拥有有效的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()); 
    } 
+0

:非常感谢... – 2012-08-10 18:45:57

+0

NetworkInterface.getNetworkInterfaces()没有列出NICs这是断开的。你测试过了吗? – 2015-12-14 21:50:42

+0

@PeterMel很久以前,我不记得了。 – 2015-12-15 13:20:35

2

的问题可能是由一个事实,即当您的计算机没有连接到互联网,你的网卡没有分配的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<NetworkInterface> e 
        = NetworkInterface.getNetworkInterfaces(); 
       e.hasMoreElements();) 
     { 
      NetworkInterface ni = e.nextElement(); 
      System.out.println(ni.getName() + " - " + formatMac(ni.getHardwareAddress())); 
     } 
    } 
} 

这应该解决的问题和工作没有任何互联网连接。

+0

@Petre:非常感谢.. – 2012-08-10 18:45:04

+0

当nic被断开时,这也不起作用! :( – 2015-12-14 21:44:34

6

试试这个应该在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(); 
    } 

} 
相关问题