How to get Server IP address in Java

In Java, you can use InetAddress.getLocalHost() to get the Ip Address of the current Server running the Java app.


package com.mkyong;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {

	public static void main(String[] args) {

	  InetAddress ip;
	  try {

		ip = InetAddress.getLocalHost();
		System.out.println("Current IP address : " + ip.getHostAddress());

	  } catch (UnknownHostException e) {

		e.e.printStackTrace();

	  }

	}

}

Output


Current IP address : 192.168.1.22

References

  1. InetAddress Java Doc
  2. How to get client Ip Address in Java

20 comments on “How to get Server IP address in Java

  1. correct the catch clause e.e.printStackTrace();
    
    Reply
  2. Hi ,

    How to get remote server (client server) IP address from soap request in java.

    Reply
  3. This example is always giving the IP Address from the /etc/hosts (linux), if we change the machine IP from static to dhcp or dhcp to static it will not give the correct result.

    Reply
  4. How i can get public Ip address of my network in which i connected.

    Reply
    1. import java.net.InetAddress;
      import java.net.UnknownHostException;

      public class getIpAddress {

      public static void main(String[] args) {
      // TODO Auto-generated method stub

      try {
      InetAddress ownIP = InetAddress.getLocalHost();
      //System.out.println(“My IP Address : “+ ownIP.getHostAddress());
      String myIp = ownIP.getHostAddress();
      System.out.println(“IP :”+myIp);
      //ownIP = InetAddress.getLoopbackAddress();
      //System.out.println(ownIP);
      } catch (UnknownHostException e) {
      e.printStackTrace();
      }

      }

      }

      Reply
  5. This doesn’t work on some version of Linux, Debian, Ubuntu, etc.

    You would have to enumerate over the interfaces.

    Reply
  6. i dont want the ip address of the my website host.. i want the ip address of my website user’s pc.. please do help..!

    Reply
    1. Hey did u find answer I also need the same but in struts 2 action….

      Reply
  7. yes its working,how to congigure 3 pc in an office using java

    Reply
      1.  final StringBuilder address = new StringBuilder(15);
        final TreeMap<Integer, java.net.NetworkInterface> sortedInterfaces = new TreeMap<Integer, java.net.NetworkInterface>();
        for (java.net.NetworkInterface networkInterface : Collections.list(java.net.NetworkInterface.getNetworkInterfaces())) {
            sortedInterfaces.put(networkInterface.getIndex(), networkInterface);
        }
        for (final java.net.NetworkInterface networkInterface : sortedInterfaces.values()) {
            if (networkInterface.isLoopback()) continue;
            for (final java.net.InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                final java.net.InetAddress inetAddress = interfaceAddress.getAddress();
                if (inetAddress instanceof java.net.Inet4Address) {
                    final java.net.Inet4Address inet4Address = (java.net.Inet4Address) inetAddress;
                    final byte[] ipAddress = inet4Address.getAddress().clone();
                    if (ipAddress.length != 4) continue;
                    for (final byte ipAddressElement : ipAddress) {
                        if (address.length() > 0) address.append('.');
                        address.append(String.format("%03d", ipAddressElement & 0xff));
                    }
                    break;
                }
            }
            if (address.length() != 0) break;
        }
        if (address.length() != 15) address.delete(0, address.length()); 
        Reply
    1. It looks like your jave code is forced to use IPv4.

      Reply

Leave a Comment

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