Getting the IP Address and Hostname of the Local Machine

try { InetAddress addr = InetAddress.getLocalHost(); // Get IP Address byte[] ipAddr = addr.getAddress(); // Get hostname String hostname = addr.getHostName(); } catch (UnknownHostException e) { }

Comments

26 Jan 2010 - 12:45pm by vgarmash (not verified)

Good example. Thank you!

28 Feb 2010 - 12:50am by Alex (not verified)

Gracias

9 Mar 2010 - 12:58pm by Anonymous (not verified)

Gracias!!!

12 Mar 2010 - 1:58pm by Anonymous (not verified)

danke szun!

16 Mar 2010 - 8:18pm by Anonymous (not verified)

This does not work on Virtual machines. Do we have any other robust way of finding machine information?

29 Mar 2010 - 3:39am by Anonymous (not verified)

Genial!

16 Apr 2010 - 9:46am by roshan india (not verified)

fine code........

22 Apr 2010 - 9:07am by andrew (not verified)

well, this gets 127.0.0.1 , what if i want to get 192.111.1.1 if i set my host ip to that ip?

25 Apr 2010 - 12:43am by Anonymous (not verified)

I want to get the address of localhost, i mean 10.0.0.2, so that i can access it from other machines.

4 May 2010 - 7:54pm by Firdi James Tommy (not verified)

thanksss

1 Jul 2010 - 7:16am by Anonymous (not verified)

try this:
String myString = addr.getHostAddress();

29 Aug 2010 - 7:08am by Ohad (not verified)

thank u !! :-)

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.