IP address of localhost in java

This example shows you how to get IP address of localhost in java.

Below is the example code of getting IP address of localhost in java:

package com.java.connect.net;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
 
//IP address of localhost in java
 
public class NetworkIPAddressExample {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();
            System.out.println("Your IP Address is : " + ip);
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        }
    }
}

The output of the above example will be as:

Your IP Address is : 10.0.0.2