我想拿到机器的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