Java: InetAddress.getLocalHost() and getHost() return fully qualified domain name (FQDN)

Solution Unverified - Updated -

Issue

  • System host name has been set to the short name and not the fully qualified domain name.
  • java.net.InetAddress.getLocalHost().getHostname() call always returns the fully qualified domain name.
  • java.net.InetAddress.getHost().getHostname() call always returns the fully qualified domain name.
  • The following sample program demonstrates this:
import java.net.InetAddress;
import java.net.UnknownHostException;

public class hostnamenew {

    public static void main(String[] argv) {
        try {
            InetAddress addr = java.net.InetAddress.getLocalHost();    
            System.out.println(addr);
            String hostname = addr.getHostName();    
            System.out.println("Hostname of system = " + hostname);
        } catch (UnknownHostException e) {
            System.out.println(e);
        }
    }
}
  • Java code getloclhost returns FQDN instead of hostname. After new GLIBC rpm installed on server, the java code getlocalhost is returning FQDN instead of hostname. How to diagnose and resolved it?

Environment

  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 6
  • java-1.6.0-openjdk
  • java-1.7.0-openjdk
  • IPv6 disabled

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content