Java Utililty Methods Local Host Get

List of utility methods to do Local Host Get

Description

The list of methods to do Local Host Get are organized into topic(s).

Method

StringgetLocalHostName()
get Local Host Name
try {
    return (InetAddress.getLocalHost()).getHostAddress();
} catch (UnknownHostException uhe) {
    String host = uhe.getMessage();
    if (host != null) {
        int colon = host.indexOf(':');
        if (colon > 0) {
            return host.substring(0, colon);
...
StringgetLocalHostName()
get Local Host Name
try {
    InetAddress addr = InetAddress.getLocalHost();
    return addr.getHostName();
} catch (Exception e) {
    return "";
StringgetLocalhostName()
get Localhost Name
String property = System.getProperty(OVERWRITE_HOSTNAME_SYSTEM_PROPERTY);
if (property != null && property.trim().length() > 0) {
    return property;
try {
    return InetAddress.getLocalHost().getHostName();
} catch (final UnknownHostException e) {
    throw new RuntimeException("unable to retrieve localhost name");
...
StringgetLocalHostName()
Retrieves the local host's hostname.
try {
    final String canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName();
    if (isFQDN(canonicalHostName)) {
        return canonicalHostName;
    } else {
        return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
...
StringgetLocalHostName()
When using the java.net.InetAddress#getHostName() method in an environment where neither a proper DNS lookup nor an /etc/hosts entry exists for a given host, the following exception will be thrown:

java.net.UnknownHostException: <hostname>: <hostname> at java.net.InetAddress.getLocalHost(InetAddress.java:1425) ...

try {
    return (InetAddress.getLocalHost()).getHostName();
} catch (UnknownHostException uhe) {
    String host = uhe.getMessage(); 
    if (host != null) {
        int colon = host.indexOf(':');
        if (colon > 0) {
            return host.substring(0, colon);
...
StringgetLocalHostName()
Best attempt to get the local hostname, with or without the existence of a DNS entry
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(':'));
...
StringgetLocalHostname()
get Local Hostname
if (localHostName_ == null)
    try {
        localHostName_ = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
return localHostName_;
StringgetLocalHostName(boolean includeDomain)
get Local Host Name
try {
    String hostname = InetAddress.getLocalHost().getHostName();
    return includeDomain ? hostname
            : hostname.indexOf(".") != -1 ? hostname.substring(0, hostname.indexOf(".")) : hostname;
} catch (Exception ex) {
    return "UNKNOWN HOST";
StringgetLocalHostName(boolean useHostname)
get Local Host Name
String hostname;
try {
    if (useHostname) {
        hostname = InetAddress.getLocalHost().getHostName();
    } else {
        hostname = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException uhe) {
...
String[]getLocalHostNames()
get Local Host Names
final Set<String> hostNames = new HashSet<String>();
hostNames.add("localhost");
try {
    final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    for (final Enumeration<NetworkInterface> ifaces = networkInterfaces; ifaces.hasMoreElements();) {
        final NetworkInterface iface = ifaces.nextElement();
        InetAddress ia = null;
        for (final Enumeration<InetAddress> ips = iface.getInetAddresses(); ips.hasMoreElements();) {
...