How to get server IP address in java

Let’s learn how to get server IP address in java.

how to get server IP address in java

To get server IP address in java use InetAddress.getLocalHost() method.

getLocalHost() method returns the address of the local host.

This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.

Here’s how to get server ip address in java.

import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetServerIPAddress
{
   public static void main(String[] args)
   {
      InetAddress ia;
      try
      {
         ia = InetAddress.getLocalHost();
         System.out.println("get server ip address: " + ia);
      }
      catch(UnknownHostException ex)
      {
         ex.printStackTrace();
      }
   }
}


Output:

get server ip address: DESKTOP-APPLEMAC/192.168.0.5


Also read – variables in java