2012-08-09 94 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(); 
    } 

} 
相關問題