• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Obtaining domain name using Java


How do I obtain the domain name of the machine I am running on using Java code?
For eg, my machine is a server whose domain name could be ec2-44-555-66-777.compute-1.amazonaws.com

I tried InetAddress.getLocalHost().getHostName() but that doesn't give me the name above. That gives me the hostname which looks similar to ip-0A11B222
0
renisenbe
Asked:
renisenbe
  • 3
  • 2
2 Solutions
 
gatorvipCommented:
Try       getCanonicalHostName() instead of getHostName()


You can also look at the docs for InetAddress here:
http://download.oracle.com/javase/6/docs/api/java/net/InetAddress.html
0
 
renisenbeAuthor Commented:
I tried InetAddress.getLocalHost().getCanonicalHostName() but it returns something like ip-0A11B222.ec2.internal
0
 
gatorvipCommented:
Then I'm not sure what you are trying to obtain.

Are you running on Windows? If so, right click on My Computer, go to Properties, then click on the Computer Name tab. Look at "full computer name" and "Domain" - how do these compare to what you want?
0
 
renisenbeAuthor Commented:
Properties does not list domain name, just the computer name. The domain name is what i type into the url. In my case it is  like ec2-44-555-66-777.compute-1.amazonaws.com
0
 
gatorvipCommented:
>>The domain name is what i type into the url.

So you're talking about an applet? If so, use getCodeBase() or getDocumentBase()
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now