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?
2 Answers
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.
4 Comments
Cameron Fredman
I liked it more when @Dan said it.
Stephen C
@CameronFredman - I could have simply commented on Dan's answer ... but I decided that what I had to add was worth a new one.
Cameron Fredman
@StephenC My apologies. I was reading that on my phone and didn't catch the InetAddress/Inet4Address distinction.
java get IP address?