Java Program to display the IP address of the system

April 8, 2022, Learn eTutorial

Here we are explaining how to write a java program to display the IP address of the system.

What is an IP address?

An IP address is an Internet Protocol address. It is an identifying number associated with a computer.


How to implement the java program to display the IP address of the system?

First, we have to declare the class IP. Then create an object of the InetAddress class as ip.Display the IP address in textual representation by using getHostAddress().The getLocalHost() function is used to get the localhost address. 


STEP 1: Declare the class IP with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Create an object of InetAddress class as ip.

STEP 4: Then display the IP address as ip. getHostAddress().


Java Source Code

public class IP 
    public static void main(String args[]) throws Exception
        InetAddress ip = InetAddress.getLocalHost();
        System.out.println("IP address of this system is := "+ip.getHostAddress());


IP address of this system is :=