code: private static String getLocalHostNameWithRecovery() throws UnknownHostException {
          try {
              return (InetAddress.getLocalHost()).getHostName();
          } catch (UnknownHostException exception) {
              String host = exception.getMessage(); // host = "hostname: hostname"
              if (host != null) {
                  int colon = host.indexOf(':');
                  if (colon > 0) {
                      return host.substring(0, colon);
                  }
              }
              throw exception;
          }
      }
docs: When using the {@link java.net.InetAddress#getHostName()} method in an
  environment where neither a proper DNS lookup nor an <tt>/etc/hosts</tt>
  entry exists for a given host, the following exception will be thrown:
  <p>
  <code>
  java.net.UnknownHostException: &lt;hostname&gt;: &lt;hostname&gt;
  at java.net.InetAddress.getLocalHost(InetAddress.java:1425)
  ...
  </code>
  </p>
  Instead of just throwing an UnknownHostException and giving up, this
  method grabs a suitable hostname from the exception and prevents the
  exception from being thrown. If a suitable hostname cannot be acquired
  from the exception, only then is the <tt>UnknownHostException</tt> thrown.
  
  @return the hostname
  @throws java.net.UnknownHostException is thrown if hostname could not be resolved