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?

2
  • 3
    Did you try asking google for java get IP address? Commented Feb 23, 2013 at 7:56
  • @home, please see How should we deal with Google questions? Commented Feb 23, 2013 at 8:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I liked it more when @Dan said it.
...or when Stephen said it here :-)
@CameronFredman - I could have simply commented on Dan's answer ... but I decided that what I had to add was worth a new one.
@StephenC My apologies. I was reading that on my phone and didn't catch the InetAddress/Inet4Address distinction.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.