4

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?

4
  • What error message do you get when it crashes? Commented Jul 6, 2011 at 11:15
  • pastebin.com/891wKcXz And yes, I have checked that it's this part. If I just put is = is + Integer.parseInt([mac address here],16); the code works fine Commented Jul 6, 2011 at 11:16
  • byte[] mac = network.getHardwareAddress(); Commented Jul 6, 2011 at 11:21
  • The "system" does not have a MAC address. A network interface does. You can have multiple NICs in a single system and therefore multiple MAC addresses as well. Commented Mar 29, 2013 at 21:19

3 Answers 3

6

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();

    }
Sign up to request clarification or add additional context in comments.

Comments

3

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 Comments

-facepalm, did NetworkInterface network = NetworkInterface.getByName("eth0"); and it worked instantly. Thanks heaps :D
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.