java

How to Get the IP Address in Java

In this tutorial, we are going to see how to write a program to get the IP address in Java. The steps to get the IP address in Java are as follows:

  • Get the local host address by calling the getLocalHost() method of the InetAddress class.
  • Get the IP address by calling the getHostAddress() method.

 

Java Program to Get the IP Address :
import java.net.InetAddress;

public class Main
{
   public static void main(String args[]) throws Exception
   {
      InetAddress ip = InetAddress.getLocalHost();
 
      System.out.print("My IP address is: ");
      System.out.println(ip.getHostAddress());
  }
}

Output:

My IP address is: 169.55.140.1
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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