Java Local Host Get getLocalhost()

Here you can find the source of getLocalhost()

Description

Gets the hostname of localhost

License

Open Source License

Exception

Parameter Description
RuntimeException When we cannot determine the local machine's hostname

Return

String

Declaration

public static String getLocalhost() throws RuntimeException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**//  www.j av  a 2s.  c o m
     * Gets the hostname of localhost
     *
     * @return String
     *
     * @throws RuntimeException
     *       When we cannot determine the local machine's hostname
     */
    public static String getLocalhost() throws RuntimeException {
        String hostname = null;

        try {
            InetAddress addr = InetAddress.getLocalHost();

            hostname = addr.getHostName();
        } catch (UnknownHostException e) {
            throw new RuntimeException("[FileHelper] {getLocalhost}: Can't get local hostname");
        }

        return hostname;
    }
}

Related

  1. getAllLocalHostNames()
  2. getCanonicalLocalHostName()
  3. getCanonicalLocalHostName()
  4. getLocalHost()
  5. getLocalHost()
  6. getLocalHost()
  7. getLocalHost()
  8. getLocalHost()
  9. getLocalHost()