Java Local Host Get getLocalHostName()

Here you can find the source of getLocalHostName()

Description

Best attempt to get the local hostname, with or without the existence of a DNS entry

License

Open Source License

Declaration

public static String getLocalHostName() 

Method Source Code

//package com.java2s;
/*/*from w  ww .  jav a  2  s.com*/
 * NetUtils.java
 *
 * This file is part of the IHMC Util Library
 * Copyright (c) 1993-2016 IHMC.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 (GPLv3) as published by the Free Software Foundation.
 *
 * U.S. Government agencies and organizations may redistribute
 * and/or modify this program under terms equivalent to
 * "Government Purpose Rights" as defined by DFARS 
 * 252.227-7014(a)(12) (February 2014).
 *
 * Alternative licenses that allow for use within commercial products may be
 * available. Contact Niranjan Suri at IHMC (nsuri@ihmc.us) for details.
 */

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**
     * Best attempt to get the local hostname, with or without the 
     * existence of a DNS entry
     */
    public static String getLocalHostName() {
        String localHostName = null;
        try {
            InetAddress addr = InetAddress.getLocalHost();
            localHostName = addr.getHostName();
        } catch (UnknownHostException uhe) {
            String host = uhe.getMessage();
            if (host != null) {
                return host.substring(0, host.indexOf(':'));
            }
            return null;
        }

        return localHostName;
    }
}

Related

  1. getLocalHostName()
  2. getLocalHostName()
  3. getLocalhostName()
  4. getLocalHostName()
  5. getLocalHostName()
  6. getLocalHostname()
  7. getLocalHostName(boolean includeDomain)
  8. getLocalHostName(boolean useHostname)
  9. getLocalHostNames()