wood burning stoves
The moose likes Sockets and Internet Protocols and the fly likes Way to get IP address through Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Way to get IP address through Java?" Watch "Way to get IP address through Java?" New topic

Way to get IP address through Java?

William Ross

Joined: Jul 06, 2006
Posts: 12
Hi, is there a way to obtain an IP address of the current box through using Java? I am creating an installation package and require the user type in their IP address. I would like the default adr in the box to be the IP of the current machine. Thanks in advance.
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Bear in mind that the IP address is not an attribute of the computer, it's an attribute of the network adapter. So if a computer has two network adapters (it's quite common these days to have an adapter where you plug an Ethernet cable in as well as a wireless adapter), then it has two IP addresses.

Also bear in mind that the IP address can vary over time. When I'm at work my laptop is assigned an IP address by the office's server, and when I take the laptop home, it's assigned a different IP address by my ISP's server.
[ August 15, 2006: Message edited by: Paul Clapham ]
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 549

And how do you retrieve the external IP on a machine ?
I have 2 IPs, one internal to my home network, and one external, the one that i get on the internet.

Check this schema to see what i mean if it's not clear :

// This retrieves the internal IP address but I want the external IP address

// Maybe something like that ??
netif = java.net.NetworkInterface.getByName("NameOfAMachineWithJVM");

Thanks in advance for your help

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I'm guessing that by "external IP address" you mean the IP address of the router or proxy server through which you are connected to the Internet. If you wanted, you could make an HTTP connection to http://whatismyipaddress.com/ and parse it out of the page you get back. At any rate, go to that page and read the FAQs. See if any of them help you.
I agree. Here's the link: http://aspose.com/file-tools
subject: Way to get IP address through Java?
jQuery in Action, 3rd edition