Java Host Name Get getDefaultHostName()

Here you can find the source of getDefaultHostName()

Description

Returns the default host name.

License

CDDL license

Return

The default host name or empty string if the host name cannot be resolved.

Declaration

public static String getDefaultHostName() 

Method Source Code

//package com.java2s;
/*//from w  w  w.  j av  a  2s. c  om
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2010 Sun Microsystems, Inc.
 * Portions copyright 2014-2016 ForgeRock AS.
 */

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    /**
     * Returns the default host name.
     *
     * @return The default host name or empty string if the host name cannot be resolved.
     */
    public static String getDefaultHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            // Fails.
        }
        String host = System.getenv("COMPUTERNAME"); // Windows.
        if (host != null) {
            return host;
        }
        host = System.getenv("HOSTNAME"); // Unix.
        if (host != null) {
            return host;
        }
        return "";
    }
}

Related

  1. getCanonicalHostName()
  2. getCanonicalHostName()
  3. getCanonicalHostName()
  4. getCasServerHostName()
  5. getCurrentHost()
  6. getFile(String host, String savePath)
  7. getFullHostname()
  8. getFullyQualifiedDomainName(String unqualifiedHostname)
  9. getFullyQualifiedHostName()