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?
Asked
Active
Viewed 1.2k times
4
-
3Did 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 Answers
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
-
-
...or when Stephen said it [here](http://stackoverflow.com/questions/9481865/how-to-get-ip-address-of-our-own-system-using-java/9482369#9482369) :-) – Arjan Feb 23 '13 at 08:42
-
2@CameronFredman - I could have simply commented on Dan's answer ... but I decided that what I had to add was worth a new one. – Stephen C Feb 23 '13 at 11:00
-
@StephenC My apologies. I was reading that on my phone and didn't catch the InetAddress/Inet4Address distinction. – Cameron Fredman Feb 23 '13 at 17:13
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