Google News
logo
Java program to Get IP Address
To get the IP address of the system in Java, you can use the InetAddress class which provides methods to get information about the network address of the local or remote hosts.

Here's a simple java program to get the IP address of the local system :
Program :
import java.net.InetAddress;

public class IPAddressExample {
    public static void main(String[] args) {
        try {
            InetAddress ip = InetAddress.getLocalHost();
            System.out.println("IP address of the local system is: " + ip.getHostAddress());
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
Output :
IP address of the local system is: 10.8.25.52
In this program, we first import the InetAddress class. We then use the getLocalHost() method of the InetAddress class to get the IP address of the local system.

Finally, we print the IP address using the getHostAddress() method of the InetAddress class.