How To Get IP Address In Java

This is simple example to demonstrate the use of InetAddress.getLocalHost() API to retrieve the IP Address details of your local system or where ever the Java program is executed. If you write a server applications, this can be used for getting the server’s IP Address. Lets look at the example code to display the IP Address of your system.

IpAddressExample.java

package javabeat.net.core;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IpAddressExample {
	public static void main(String[] args) {
		InetAddress ipAddress = null;
		try{
			ipAddress=InetAddress.getLocalHost();
		}catch (UnknownHostException exception){
			exception.printStackTrace();
		}
		System.out.println("IP Address : "+ipAddress.getHostAddress());
	}
}

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better