Take the 2-minute tour ×

How would I get a users IP address?

InetAddress ip;
try {

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

} catch (UnknownHostException e) {



That returns:

I know that is not my IP. It is my local IP address. How do I get a users IP using java..?

share|improve this question

4 Answers 4

up vote 7 down vote accepted

The shortest method is:

try {
InetAddress thisIp =InetAddress.getLocalHost();
catch(Exception e) {

However getLocalHost docs say:

If there is a security manager, its checkConnect method is called with the local host name and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, an InetAddress representing the loopback address is returned.

and in some cases InetAddress.getLocalHost() doesn't consult your interfaces, it simply returns constant (for IPv4))

I think NetworkInterface.getNetworkInterfaces is what you need to enumerate all the possibilities. Here's an example which doesn't show virtual addresses, but works for "main" interfaces:

import java.net.*;
import java.util.*;

public class Test
    public static void main(String[] args)
        throws Exception // Just for simplicity
        for (Enumeration<NetworkInterface> ifaces = 
             ifaces.hasMoreElements(); )
            NetworkInterface iface = ifaces.nextElement();
            System.out.println(iface.getName() + ":");
            for (Enumeration<InetAddress> addresses =
                 addresses.hasMoreElements(); )
                InetAddress address = addresses.nextElement();
                System.out.println("  " + address);


Try using this (First output should be IP after PC name):

    InetAddress[] localaddr;

    String computername = null;

    try {
        computername = InetAddress.getLocalHost().getHostName();//get pc name
    } catch (UnknownHostException ex) {


    try {
        localaddr = InetAddress.getAllByName(computername);
        for (int i = 0; i < localaddr.length; i++) {
            System.out.println("\n" + localaddr[i].getHostAddress());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block


share|improve this answer
That prints a bunch of stuff. I don't really see an IP adress though.. –  user1529476 Aug 13 '12 at 19:42
Would my IP be : xx.xx.x.xxx That is the third line in the output. –  user1529476 Aug 13 '12 at 19:47
@HenryHarris I'm not sure... what the above method does is loop through all your network interfaces and print out their addresses. Find out your IP then check if you see it in the output whatsmyip.org –  David Kroukamp Aug 13 '12 at 19:50
It doesn't find my IP.. O.o on the IP lookup. –  user1529476 Aug 13 '12 at 19:52
My IP is no where found in the output.. –  user1529476 Aug 13 '12 at 19:57

When you call getLocalHost() you are asking for the relative address of the router you are connected to, which is (as expected) To determine the IP address using InetAddress, try:


There's also a getAllByName(String) method which might serve your purpsoes. Read the javadoc.


share|improve this answer
What would be my URL...? –  user1529476 Aug 13 '12 at 19:53

It get IP address of your machine ,

    InetAddress inet= InetAddress.getLocalHost();
    String str[]=inet.toString().split("/");
    System.out.println(inet.toString() +" "+ str[i]);
    InetAddress[] inetAd=InetAddress.getAllByName(str[0]);
    for(int j=0;j<inetAd.length;j++ ){
share|improve this answer

This Will return your current used interface address:

NetworkInterface ni;
try {
    Enumeration<NetworkInterface> interfaces NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        ni = interfaces.nextElement();
        if (ni.isUp()) {
            Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress ia = inetAddresses.nextElement();
                if (!ia.isLinkLocalAddress()) {
                    return ia;
} catch (SocketException ex) {
    throw new RuntimeException(ex);
share|improve this answer

Your Answer

