9 Replies Latest reply: Sep 9, 2010 4:47 AM by 843790 RSS

    get the mac addresses of the localhost

    843790
      hi,
      i am trying to get all the mac address of the localhost network interfaces.
      here is my code. i don't seem to find what is wrong with it.

      i will appreciate any help.
      thanks
      import java.net.InetAddress;
      import java.net.NetworkInterface;
      import java.net.SocketException;
      import java.net.UnknownHostException;
      
      public class MAC {
           public static void main(String[] args) {
                try { 
                     InetAddress[] addrs = null;
                     if((addrs = InetAddress.getAllByName("localhost")) == null) System.exit(1);
                     
                     NetworkInterface ni = null;
                     for(int i = 0; i < addrs.length; ++i) {
                          if((ni = NetworkInterface.getByInetAddress(addrs)) == null)      System.exit(2);
                          
                          byte[] mac = null;
                          if((mac = ni.getHardwareAddress()) == null) System.exit(3);
                          
                          System.out.println(addrs[i]);
                          System.out.println(new String(mac));
                     }
                }
                catch(UnknownHostException e) { System.out.println(e.getMessage()); }
                catch(SocketException e) { System.out.println(e.getMessage()); }
           }
      }
      Edited by: BigJ___ on Sep 9, 2010 2:44 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: get the mac addresses of the localhost
          843790
          Could you describe what the problem is?

          The first thing I see is "new String(mac)". It's definitely not what you want, as the byte-array doesn't contain text data. You'll need to convert each byte to a hex string, if you want to achieve the common way of printing a mac address.
          • 2. Re: get the mac addresses of the localhost
            843790
            here is more readable version of the code
            import java.net.InetAddress;
            import java.net.NetworkInterface;
            import java.net.SocketException;
            import java.net.UnknownHostException;
            
            public class MAC {
                 public static void main(String[] args) {
                      try { 
                           InetAddress[] addrs = InetAddress.getAllByName("localhost");
                           if(addrs == null) {
                                System.err.println("code 1");
                                System.exit(1);
                           }
                           
                           NetworkInterface ni = null;
                           for(int i = 0; i < addrs.length; ++i) {
                                ni = NetworkInterface.getByInetAddress(addrs);
                                if(ni == null) {
                                     System.err.println("code 2");
                                     System.exit(2);
                                }
                                
                                byte[] mac = ni.getHardwareAddress();
                                if(mac == null) {
                                     System.err.println("code 3");
                                     System.exit(3);
                                }
                                System.out.println(addrs[i]);
                                System.out.println(new String(mac));
                           }
                      } catch(UnknownHostException e) {
                           System.out.println(e.getMessage());
                      } catch(SocketException e) {
                           System.out.println(e.getMessage());
                      }
                 }
            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
            • 3. Re: get the mac addresses of the localhost
              843790
              thanks for your fast reply.
              the thing is, that the length of the byte's array is zero.

              strange...
              have another idea?
              • 4. Re: get the mac addresses of the localhost
                843790
                BigJ___ wrote:
                thanks for your fast reply.
                the thing is, that the length of the byte's array is zero.
                Well, the interface that reacts to "localhost" is usually the loopback device, which doesn't have a MAC address, so that might be your reason.
                • 5. Re: get the mac addresses of the localhost
                  843790
                  so how should i resolve the mac address of a network interface on the local machine?
                  • 6. Re: get the mac addresses of the localhost
                    843790
                    JoachimSauer wrote:
                    BigJ___ wrote:
                    thanks for your fast reply.
                    the thing is, that the length of the byte's array is zero.
                    Well, the interface that reacts to "localhost" is usually the loopback device, which doesn't have a MAC address, so that might be your reason.
                    so how should i resolve the mac address of a network interface on the local machine?
                    • 7. Re: get the mac addresses of the localhost
                      EJP
                      You have 'resolved' it. You just haven't printed it correctly. It's not character data so you can't make a String out of it. Try new BigDecimal(mac).toHexString() or whatever it is.
                      • 8. Re: get the mac addresses of the localhost
                        843790
                        ejp wrote:
                        You have 'resolved' it. You just haven't printed it correctly. It's not character data so you can't make a String out of it. Try new BigDecimal(mac).toHexString() or whatever it is.
                        i think that's how i should do it.
                        String macAddr = "";
                        for(int j = 0; j < mac.length; ++j)
                             macAddr += Integer.toHexString(mac[j] & 0xFF);
                        System.out.println(macAddr.toUpperCase());
                        but in my case, when specifying at the beginning of the code "localhost" the length of the byte's array is zero. whereas, when i specify the binding address of the network interface, the relevant mac address is returned.
                        • 9. Re: get the mac addresses of the localhost
                          EJP
                          I think you should read what's been posted here.