Java Host Name Get getHostname()

Here you can find the source of getHostname()

Description

Determine the hostname of the machine Kettle is running on

License

Apache License

Return

The hostname

Declaration

public static final String getHostname() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.InetAddress;

import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    /**/*from www  . java 2 s  .c  o  m*/
     * Determine the hostname of the machine Kettle is running on
     * 
     * @return The hostname
     */
    public static final String getHostname() {
        String lastHostname = "localhost";
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface nwi = en.nextElement();
                Enumeration<InetAddress> ip = nwi.getInetAddresses();

                while (ip.hasMoreElements()) {
                    InetAddress in = (InetAddress) ip.nextElement();
                    lastHostname = in.getHostName();
                    // System.out.println("  ip address bound : "+in.getHostAddress());
                    // System.out.println("  hostname         : "+in.getHostName());
                    // System.out.println("  Cann.hostname    : "+in.getCanonicalHostName());
                    // System.out.println("  ip string        : "+in.toString());
                    if (!lastHostname.equalsIgnoreCase("localhost") && !(lastHostname.indexOf(':') >= 0)) {
                        return lastHostname;
                    }
                }
            }
        } catch (SocketException e) {

        }

        return lastHostname;
    }
}

Related

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