Java Host Name Get getHostName()

Here you can find the source of getHostName()

Description

get Host Name

License

Open Source License

Declaration

public static String getHostName() 

Method Source Code

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

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    public static String getHostName() { // Written largely by Trevor.
        try {// w  ww  . j av a2s  .  c  o  m
            InetAddress addr = InetAddress.getLocalHost();
            String hostName = addr.getHostName();
            if (hostName == null) {
                return "unknownHost";
            }
            int locFirstPeriod = hostName.indexOf('.');
            if (locFirstPeriod > 0) { // Not sure what a leading period would be, but keep it if this case ever occurs.
                return hostName.substring(0, locFirstPeriod);
            }
            return hostName;
        } catch (UnknownHostException e) {
            return "unknownHost";
        }
    }
}

Related

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