Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 21-01-2010
Member
 
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.

Reply With Quote
  #2  
Old 21-01-2010
Member
 
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();
        }
    }
}
Reply With Quote
  #3  
Old 21-01-2010
Member
 
Join Date: May 2008
Posts: 2,007
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.
Reply With Quote
  #4  
Old 21-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
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) ? "-" : "");
    }
  }
}
Reply With Quote
  #5  
Old 21-01-2010
Member
 
Join Date: May 2008
Posts: 2,291
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);
        }
    }
}
Reply With Quote
  #6  
Old 20-04-2011
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to get MAC address of a host using java program?"
Thread Thread Starter Forum Replies Last Post
Forward all emails from one mailbox to external SMTP Host or Ip Address safnha Windows Software 1 22-11-2011 11:51 PM
Finding the IP address of another host in the network Benjhonson Networking & Security 5 03-12-2010 07:47 AM
(Exim 4.24) No host name found for IP address RedIndigo Networking & Security 1 16-02-2009 09:30 PM
Is there any way to fetch the ip address/Host name of the Domain controller Swati_here_2008 Software Development 4 20-05-2008 03:51 PM
Reload DNS from AD recovers wrong IP address for host - pls help karrma Active Directory 1 06-01-2007 05:42 PM


All times are GMT +5.5. The time now is 04:04 PM.