Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get mac and ip address using java
06-14-2009, 10:17 PM
Post: #1
how to get mac and ip address using java
hello,
please help me, i need to get the mac and ip address of my pc using java program for a project.
thanks in advance.
Find all posts by this user
Quote this message in a reply
06-15-2009, 01:59 AM
Post: #2
RE: how to get mac and ip address using java
(06-14-2009 10:17 PM)rahuljin Wrote:  hello,
please help me, i need to get the mac and ip address of my pc using java program for a project.
thanks in advance.

To get the IP address try the following code.

Code:
java.net.InetAddress address = java.net.InetAddress.getLocalHost();
System.out.println("IP Addres  :"+address.getHostAddress());

To get the Mac address try this.
Code:
java.net.NetworkInterface networkInterface =  java.net.NetworkInterface.getByInetAddress(address);
        byte[] macAddress = networkInterface.getHardwareAddress();

        for(int index=0; index < macAddress.length; index++){
            System.out.format("%02X%s", macAddress[index], (index < macAddress.length - 1) ? "-" : "");
        }
Visit this user's website Find all posts by this user
Quote this message in a reply
06-15-2009, 02:06 AM (This post was last modified: 06-15-2009 02:07 AM by rahuljin.)
Post: #3
RE: how to get mac and ip address using java
thanks for the help.
Find all posts by this user
Quote this message in a reply
10-08-2009, 01:28 PM (This post was last modified: 10-08-2009 01:44 PM by albinjoseph.)
Post: #4
RE: how to get mac and ip address using java
MAC addresses are used on a local networking, for example, between two hosts to communicate basic data. This does not include Internet traffic. However, once I want to visit Experts-Exchange, my addressing must be converted to IP. In other words, the MAC address of Experts-Exchange is foreign to me. Please keep this in mind for whatever your application may be.
Find all posts by this user
Quote this message in a reply
03-12-2010, 09:11 PM
Post: #5
RE: how to get mac and ip address using java
It is very easy process to get MAC address using java code. Just try to run following code in your PC.

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) ? "-" : "");
}
}
}

acekard 2
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: