Articles → Java → Get IP address and MAC address in Java

Get IP address and MAC address in Java






Code


package com.TestProject;

import java.net.InetAddress;
import java.net.NetworkInterface;


public class TestClass {

	public static void main(String[] args) {

		try
		{
			InetAddress ip = InetAddress.getLocalHost();
			System.out.println("Current IP address : " + ip.getHostAddress());
			
			NetworkInterface nwi = NetworkInterface.getByInetAddress(ip);			
			byte[] mac = nwi.getHardwareAddress();
			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("MAC address:" + sb.toString());
		}
		catch(Exception ex)
		{
			System.out.println(ex.getMessage());
		}
		
	}
}



Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, June 21, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250