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

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

20 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Thirumal
10 years ago

it returns 127.0.0.1

Deepti Vyas
5 years ago
correct the catch clause e.e.printStackTrace();
suresh
8 years ago

Hi ,

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

Dom1x
8 years ago

Great, it works

henry
12 years ago

Can I get others server IP in LAN. like samba server ?

rauthan
13 years ago

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.

Anand
13 years ago

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

David
13 years ago

How would I save the IP address to a string or write it to a file?

Anand
13 years ago
Reply to  David

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();
}

}

}

David
13 years ago
Reply to  Anand

Thank you sir! it J’s much appreciated!

rosalia
7 years ago
Reply to  David

alguien sabe mascara de subred
codigo

Gerardo Ramirez
13 years ago

Thanks 😀

Jamie
13 years ago

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

You would have to enumerate over the interfaces.

santhosh
13 years ago

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..!

Vishal
13 years ago
Reply to  santhosh

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

herbert
14 years ago

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

Soroush
14 years ago

Hi
Only return localhost IP
Current IP address : 127.0.1.1

Siyu Jiang
11 years ago
Reply to  Soroush

It looks like your jave code is forced to use IPv4.

Sandee N
14 years ago
Reply to  Soroush

Even i have the same problem!!!!!! Need Help

pes
13 years ago
Reply to  Sandee N
 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());