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