Results 1 to 6 of 6

Thread: How to get MAC address of a host using java program?

  1. #1
    Join Date
    Nov 2009
    Posts
    131

    How to get MAC address of a host using java program?

    Hello to all,
    I am new to this forum. I am last year B.Sc.I.T. student. In our last exam one question was asked like How to get MAC address of a host using java program? None of us able to write correct answer. Anyone know solution of this program. Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to get MAC address of a host using java program?

    You can obtain a MAC address using following code. Just go through it.


    Code:
    package org.kodejava.example.net;
     
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
     
    public class MacAdd {
     
        public static void main(String[] args) {
            try {
                
                InetAddress add = InetAddress.getByName("192.168.46.53");
     
               
                NetworkInterface ni1 = NetworkInterface.getByInetAddress(add);
                if (ni != null) {
                    byte[] mac1 = ni1.getHardwareAddress();
                    if (mac1 != null) {
                        
                        for (int k = 0; k < mac.length; k++) {
                            System.out.format("%02X%s", mac[k], (i < mac.length - 1) ? "-" : "");
                        }
                    } else {
                        System.out.println("Address doesn't exist ");
                    }
                } else {
                    System.out.println("address is not found.");
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to get MAC address of a host using java program?

    As per my knowledge I don't think it's possible in a pure java solution. I general Products like FlexLM do it this work as part of their functionality, but they somewhat delegate the actual reading of the MAC address to a native app. As per my knowledge the the problem is that retrieving the MAC address is very different on different platforms, so the native app is rewritten for each platform, and distributed with the client.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to get MAC address of a host using java program?

    It is very easy process to get MAC address using java code. Just try to run following code in your PC.
    Code:
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        InetAddress addr = InetAddress.getLocalHost();
    
        NetworkInterface ni = NetworkInterface.getByInetAddress(addr);
        byte[] maca = ni.getHardwareAddress();
    
        for (int k = 0; k < maca.length; k++) {
          System.out.format("%02X%s", maca[k], (k < maca.length - 1) ? "-" : "");
        }
      }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to get MAC address of a host using java program?

    The common way in Java 5 was to start a native process to run ipconfig or ifconfig and parse the OutputStream to get MAC address of a host.


    Code:
    private String getMacAddress() throws IOException {
        String com = “ipconfig /all”;
        Process pid1 = Runtime.getRuntime().exec(command);
        BufferedReader inn = new BufferedReader(new InputStreamReader(pid1.getInputStream()));
        Pattern pp = Pattern.compile(”.*Physical Addres(MAC)s.*: (.*)”);
        while (true) {
            String line = inn.readLine();
            if (line == null)
                break;
            Matcher mm = pp.matcher(line);
            if (mm.matches()) {
                return mm.group(1);
            }
        }
    }

  6. #6
    Join Date
    Apr 2011
    Posts
    1

    Re: How to get MAC address of a host using java program?

    hey will help me to retrieve the MAC address of remote machine .....pls its urgent

Similar Threads

  1. Replies: 1
    Last Post: 22-11-2011, 11:51 PM
  2. Finding the IP address of another host in the network
    By Benjhonson in forum Networking & Security
    Replies: 5
    Last Post: 03-12-2010, 07:47 AM
  3. (Exim 4.24) No host name found for IP address
    By RedIndigo in forum Networking & Security
    Replies: 1
    Last Post: 16-02-2009, 09:30 PM
  4. Is there any way to fetch the ip address/Host name of the Domain controller
    By Swati_here_2008 in forum Software Development
    Replies: 4
    Last Post: 20-05-2008, 02:51 PM
  5. Replies: 1
    Last Post: 06-01-2007, 05:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,613,847.29243 seconds with 17 queries