A blog for technology, SEO tips, website development and open source programming.

Get Mac Address and IP Address using JAVA

0 4,045

 

How to get Mac Address using JAVA?

 

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MacAddress {

public static void main(String[] args) throws SocketException{

InetAddress ip;
try {

ip = InetAddress.getLocalHost();
System.out.println(“Current IP address : ” + ip.getHostAddress());

NetworkInterface network = NetworkInterface.getByInetAddress(ip);

byte[] mac = network.getHardwareAddress();

System.out.print(“Current 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());

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (SocketException e){

e.printStackTrace();

}

}

}

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More