Share

August 8, 2010

Java: Get HostName without a Request

Sometime you need the hostname but do not have the request handy...
 
 public static String getHostName() {
        String hostName = "localhost";
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            // NO OP we will just use localhost
        }
        return hostName;
    }