Java Host Name Get getHostname()

Here you can find the source of getHostname()

Description

get Hostname

License

Open Source License

Declaration

public static String getHostname() 

Method Source Code

//package com.java2s;
/*/*from  www . j a v a2s .  c  o m*/
 * License information at https://github.com/Caltech-IPAC/firefly/blob/master/License.txt
 */

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    public static String getHostname() {
        String retHost = "UNKNOWN_HOST";
        try {
            String host = InetAddress.getLocalHost().getCanonicalHostName();
            if (host != null) {
                int idx = host.indexOf(".");
                if (idx > -1) {
                    retHost = host.substring(0, idx);
                    try {
                        int v = Integer.parseInt(retHost);
                        retHost = host;
                    } catch (NumberFormatException e) {
                        // do nothing, we found a host name
                    }

                } else {
                    retHost = host;
                }
            }
        } catch (UnknownHostException e) {
            retHost = "UNKNOWN_HOST";
        }
        return retHost;
    }
}

Related

  1. getHostname()
  2. getHostName()
  3. getHostName()
  4. getHostname()
  5. getHostName()
  6. getHostName(Proxy proxy)
  7. getHostName(String destination)
  8. getHostName(String host)
  9. getHostname(String targetEndpoint)