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;
    }

1 comment:

  1. May not work, if you have a little more exotic network with non transparent proxies between client and server.

    ReplyDelete