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