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());
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>