0

I have a program which requires it to know it's IP Address. However when I use

InetAddress current_addr = addresses.nextElement();

It returns

127.0.1.1

Which isn't very helpful. How can I get my non-local IP Address from java?

user760220
  • 1,207
  • 2
  • 13
  • 12
  • This question has been already answered many times before. For example refer to http://stackoverflow.com/questions/18254848/getting-ip-in-java/18255093#18255093 – vinay Aug 16 '13 at 17:05
  • Have you considered trying a few more elements? – user207421 Aug 17 '13 at 00:16

1 Answers1

1

What do you get when you use:

InetAddress IP = InetAddress.getLocalHost();
String ipAddress  = IP.getHostAddress();

it should ideally give you the ip address if you don't have more than one network interfaces.

I tested it locally and it gives me proper ip address of my machine i.e.

192.168.2.10

If you have more than one network interface then you can try to use the NetworkInterface class, here is the sample:

  Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
            for (; n.hasMoreElements();)
            {
                    NetworkInterface e = n.nextElement();
                    System.out.println("Interface: " + e.getName());
                    Enumeration<InetAddress> a = e.getInetAddresses();
                    for (; a.hasMoreElements();)
                    {
                            InetAddress addr = a.nextElement();
                            System.out.println("  " + addr.getHostAddress());
                    }
            }

Source taken from a related post: java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP?

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • I think the questioner is looking for external IP address – vinay Aug 16 '13 at 17:05
  • That will only return the IP assigned by your router. – Josh M Aug 16 '13 at 17:07
  • @JoshM No it will return your machine address. I have tested this and it returns 192.168.2.10 while my router address is 192.168.2.1 – Juned Ahsan Aug 16 '13 at 17:08
  • @JunedAhsan He means you'll only get the private, non-routable IP your router is assigning (192.168.2.10 in your example), not the real external IP the router is assigned from the ISP. It's unclear which the OP is asking about. – Brian Roach Aug 16 '13 at 17:13
  • @BrianRoach I interpreted the question to know the local ip address as he mentioned in the problem that he is localhost address i.e. 127.0.0.1 – Juned Ahsan Aug 16 '13 at 17:14
  • @JunedAhsan User is able to get local IP and is not interested. I think it was very clear. – vinay Aug 16 '13 at 17:23
  • 1
    @VinayC Maybe very clear to you but not to me. If the user question need to be clear then he should share a subnetted address rather than localhost address. And then asking for public ip address. – Juned Ahsan Aug 16 '13 at 17:25
  • That prints "127.0.1.1" on my ubuntu machine - *nix system usually have more than one interface. – zapl Aug 16 '13 at 17:38
  • @zapl check my updated answer and try that code. – Juned Ahsan Aug 16 '13 at 17:43
  • @JunedAhsan He said "How can I get my non-local IP Address from java?" I think it is very clear. Anyways I am out of this. – vinay Aug 16 '13 at 17:47
  • To clarify I would like the external address. Could it be a linux thing? – user760220 Aug 16 '13 at 18:06
  • works to print everything, see http://pastebin.com/uei5fMvp (+ code that should find a useful local address in more cases, at least for me) - @user760220 It's not a linux thing, the external address of the network you are in (i.e. the address that represents your entire local network on the internet) is not known to devices in that network. Only the gateway thing (= router in most cases) that is directly connected to the internet knows the address. do `ifconfig` in a shell and you see all the addresses that are known for your local system. – zapl Aug 16 '13 at 18:19