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
renisenbeAsked:
Who is Participating?
 
gatorvipConnect With a Mentor Commented:
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
 
gatorvipConnect With a Mentor Commented:
>>The domain name is what i type into the url.

So you're talking about an applet? If so, use getCodeBase() or getDocumentBase()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.