Obtaining domain name using Java

Posted on 2011-05-04
Last Modified: 2012-05-11

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

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
Question by:renisenbe
    LVL 20

    Accepted Solution

    Try       getCanonicalHostName() instead of getHostName()

    You can also look at the docs for InetAddress here:

    Author Comment

    I tried InetAddress.getLocalHost().getCanonicalHostName() but it returns something like ip-0A11B222.ec2.internal
    LVL 20

    Expert Comment

    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?

    Author Comment

    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
    LVL 20

    Assisted Solution

    >>The domain name is what i type into the url.

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

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now