Here you can find the source of getHostName()
public static String getHostName()
//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"; } } }