问题
I'm trying to get the MAC address of a linux system with this code:
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();
}
But it just crashes... does anyone know why?
回答1:
From your comments, clearly network
is null
, which means that getByInetAddress()
could not find an interface with that IP address (see the JavaDocs: http://download.oracle.com/javase/1.5.0/docs/api/java/net/NetworkInterface.html#getByInetAddress(java.net.InetAddress)).
回答2:
You might have more than one network interface and I would not count on the interface's name. I suggest you to go over all the interfaces and look for one that has a MAC address. You can use this example as a base line:
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();
}
回答3:
Just a small modification of the code of Guy :
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;
}
This code has been tested on Windows, Linux (Ubuntu) and Mac OS X with success.
Because the network can be null, I ignore all null cases and I also ignore empty addresses. I think that if we don't do this, we view the crash. I choose the first found address and it works but it may be wrong, so just test it.
来源:https://stackoverflow.com/questions/6595479/java-getting-mac-address-of-linux-system