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
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Thirumal
Guest
Thirumal

it returns 127.0.0.1

suresh
Guest
suresh

Hi ,

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

Dom1x
Guest
Dom1x

Great, it works

henry
Guest
henry

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

rauthan
Guest
rauthan

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
Guest
Anand

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

David
Guest
David

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

Anand
Guest
Anand

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
Guest
David

Thank you sir! it J’s much appreciated!

rosalia
Guest
rosalia

alguien sabe mascara de subred
codigo

trackback
How to get client Ip Address in Java

[…] How to get Server IP address in Java […]

Gerardo Ramirez
Guest
Gerardo Ramirez

Thanks 😀

Jamie
Guest
Jamie

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

You would have to enumerate over the interfaces.

santhosh
Guest
santhosh

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
Guest
Vishal

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

herbert
Guest
herbert

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

Soroush
Guest
Soroush

Hi
Only return localhost IP
Current IP address : 127.0.1.1

Siyu Jiang
Guest
Siyu Jiang

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

Sandee N
Guest
Sandee N

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

pes
Guest
pes
 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());