Java Host Name Get getHostname()

Here you can find the source of getHostname()

Description

get Hostname

License

Apache License

Return

Hostname of this computer, e.g. hostname.domain.tld

Declaration

public static String getHostname() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**//from   w  w  w.j a va 2  s .c o m
     * @return Hostname of this computer, e.g. {@code hostname.domain.tld}
     */
    public static String getHostname() {
        String hostName;
        final InetAddress localHost;
        try {
            localHost = InetAddress.getLocalHost();
            hostName = localHost.getHostName();
        } catch (UnknownHostException ignore) {
            hostName = "localhost";
        }
        return hostName;
    }
}

Related

  1. getHostName()
  2. getHostName()
  3. getHostname()
  4. getHostName()
  5. getHostName()
  6. getHostName()
  7. getHostName()
  8. getHostname()
  9. getHostname()