2011-07-06 78 views
3

我想這個代碼來獲得一個Linux系統的MAC地址的獲取MAC地址:爪哇 - Linux系統

try { 
    ip = InetAddress.getLocalHost(); 
    NetworkInterface network = NetworkInterface.getByInetAddress(ip); 
    byte[] mac = network.getHardwareAddress(); 
    // System.out.print("Current MAC address: "); 
    for (int i = 0; i < mac.length; i++) { 
    is = is + Integer.parseInt(
     String.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : ""),16); 
    } 
} catch (UnknownHostException e) { 
    e.printStackTrace(); 
} catch (SocketException e) { 
    e.printStackTrace(); 
} 

,但它只是崩潰......沒有人知道爲什麼嗎?

+0

當它崩潰時,你會得到什麼錯誤信息? –

+0

http://pastebin.com/891wKcXz是的,我已檢查過它是這部分。如果我只是把= is + Integer.parseInt([mac address here],16);代碼工作正常 – y0u

+0

哪一行是133行? –

回答

3

從您的意見,明確networknull,這意味着getByInetAddress()找不到與IP地址的接口(參考JavaDoc:http://download.oracle.com/javase/1.5.0/docs/api/java/net/NetworkInterface.html#getByInetAddress(java.net.InetAddress))。

+2

-facepalm,NetworkInterface network = NetworkInterface.getByName(「eth0」);它立即起作用。謝謝堆:D – y0u

+0

FYI http://www.includehelp.com/java-programs/get-system-mac-address-of-windows-or-linux-machine.aspx – gihan

5

您可能有多個網絡接口,我不會指望接口的名稱。我建議你瀏覽所有的接口並尋找一個具有MAC地址的接口。 你可以用這個例子作爲一個底線:

try { 

     Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); 
     while(networkInterfaces.hasMoreElements()) 
     { 
      NetworkInterface network = networkInterfaces.nextElement(); 
      System.out.println("network : " + network); 
      byte[] mac = network.getHardwareAddress(); 
      if(mac == null) 
      { 
       System.out.println("null mac");    
      } 
      else 
      { 
       System.out.print("MAC address : "); 

       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()); 
       break; 
      } 
     } 
    } catch (SocketException e){ 

     e.printStackTrace(); 

    } 
2

蓋伊的代碼只是一個小改動:

public String searchForMac() throws SocketException { 
    String firstInterface = null;   
    Map<String, String> addressByNetwork = new HashMap<>(); 
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); 

    while(networkInterfaces.hasMoreElements()){ 
     NetworkInterface network = networkInterfaces.nextElement(); 

     byte[] bmac = network.getHardwareAddress(); 
     if(bmac != null){ 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < bmac.length; i++){ 
       sb.append(String.format("%02X%s", bmac[i], (i < bmac.length - 1) ? "-" : ""));   
      } 

      if(sb.toString().isEmpty()==false){ 
       addressByNetwork.put(network.getName(), sb.toString()); 
       System.out.println("Address = "+sb.toString()+" @ ["+network.getName()+"] "+network.getDisplayName()); 
      } 

      if(sb.toString().isEmpty()==false && firstInterface == null){ 
       firstInterface = network.getName(); 
      } 
     } 
    } 

    if(firstInterface != null){ 
     return addressByNetwork.get(firstInterface); 
    } 

    return null; 
} 

此代碼已經在Windows,Linux(Ubuntu的)和Mac OS測試X成功。

因爲網絡可以爲空,所以我忽略了所有的空情況,我也忽略空地址。我認爲,如果我們不這樣做,我們認爲這次崩潰。我選擇了第一個發現的地址,它可以工作,但它可能是錯誤的,所以只需測試它。