14

How do I obtain the domain name of the machine I am running on using Java?
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

2
  • 1
    Perhaps this might help you a little (have never tried this myself): blogs.captechconsulting.com/blog/david-tiller/… Commented May 4, 2011 at 23:46
  • I just edited my question. I guess i was really looking for the domain name. Commented May 4, 2011 at 23:51

5 Answers 5

14

I guess you can try InetAddress.getCanonicalHostName() or InetAddress.getName() methods. Assuming there is a proper name service running on your net these two should do the trick.

The JavaDocs for getCanonicalHostName() says

Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration.

So if you want to get your local FQDN, you can typically call: InetAddress.getLocalHost().getCanonicalHostName()

Sign up to request clarification or add additional context in comments.

Comments

7

getCanonicalHostName gives you the fully qualified domain name. I have tried using InetAddress.getLocalHost().getHostname() but it just gets the hostname value you see in command line which may or may not contain the fully qualified name.

To check if the fully qualified domain name is set using command line (in linux), use hostname --fqdn.

getCanonicalHostName

public String getCanonicalHostName() Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration.

/** Main.java */ import java.net.InetAddress; public class Main { public static void main(String[] argv) throws Exception { byte[] ipAddress = new byte[] {(byte)127, (byte)0, (byte)0, (byte)1 }; InetAddress address = InetAddress.getByAddress(ipAddress); String hostnameCanonical = address.getCanonicalHostName(); System.out.println(hostnameCanonical); } } 

Example is taken from: http://www.java2s.com/Tutorials/Java/java.net/InetAddress/Java_InetAddress_getCanonicalHostName_.htm

Comments

-2

Best way to get a domain from a given uri in java , below

java.net.URI uri = new URI(url); String host = uri.getHost(); InternetDomainName internetDomainName = InternetDomainName.from(host); String domainName = internetDomainName.topPrivateDomain().toString(); 

1 Comment

If you are referring to this InternetDomainName class, I think you should mention that the class is part of [Google] guava.
-5

Do you really need the domain name, or is IP address sufficient? If latter, try using InetAddress.getLocalHost().getHostAddress()

Comments

-6

I had the same problem today and found this very easy solution:

 System.getenv("userdomain"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.