4

Is there a way that I can get the ip address of a machine in Java? To get the IP address from the machine where my jar file is running?

robertdi
  • 178
  • 2
  • 3
  • 11
  • 3
    Did you try asking google for `java get IP address`? – home Feb 23 '13 at 07:56
  • @home, please see [How should we deal with Google questions?](http://meta.stackexchange.com/questions/8724/how-should-we-deal-with-google-questions) – Arjan Feb 23 '13 at 08:30

2 Answers2

5

The best answer is:

InetAddress.getLocalHost().getHostAddress();

(The getLocalHost method is declared in InetAddress not Inet4Address.)

It is also worth nothing that getLocalHost() does some security checks, so this may not work if your JAR is run in a sandbox.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

You can get the IP address of your machine using the following call:

Inet4Address.getLocalHost().getHostAddress();

The above is in the Java APIs, so you don't need any jar for that.

Dan D.
  • 32,246
  • 5
  • 63
  • 79