Java Host Name Get getHostName()

Here you can find the source of getHostName()

Description

returns the lowercase hostname

License

Mozilla Public License

Declaration

public static String getHostName() 

Method Source Code

//package com.java2s;
/**/*from w  w w.j  a v a2s  .  co m*/
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * If it is not possible or desirable to put the notice in a particular file,
 * then You may include the notice in a location (such as a LICENSE file in a
 * relevant directory) where a recipient would be likely to look for such a
 * notice.
 *
 * 
 */

import java.net.InetAddress;

public class Main {
    private static String myHostname = null;

    /**
     * returns the lowercase hostname
     *
     * @return
     */
    public static String getHostName() {
        if (myHostname == null) {
            try {
                InetAddress addr = InetAddress.getLocalHost();

                // Get IP Address
                // byte[] ipAddr = addr.getAddress();
                // Get hostname
                myHostname = addr.getHostName().toLowerCase();
            } catch (Exception e) {
                myHostname = "ADDRESS_UNKNOWN";
            }
        }
        return myHostname;
    }
}

Related

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