我想拿到機器的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();
}
}
}
你需要開始縮進你的代碼工作。由於缺乏縮進,現在很難閱讀! – ThiefMaster 2012-08-09 13:39:18
如果你的盒子沒有連接到互聯網會發生什麼?輸出是什麼? – home 2012-08-09 13:40:55
[獲取本地計算機上的MAC地址與Java]的可能的重複(http://stackoverflow.com/questions/6164167/get-mac-address-on-local-machine-with-java) – 2012-08-09 13:45:27