Here you can find the source of getLocalHostName()
public static String getLocalHostName() throws SocketException, UnknownHostException
//package com.java2s; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class Main { /**//from ww w . java2 s.c o m * Retrieves the localhost's hostname, or if unavailable, the ip address */ public static String getLocalHostName() throws SocketException, UnknownHostException { try { return InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { NetworkInterface networkInterface = NetworkInterface.getNetworkInterfaces().nextElement(); if (networkInterface == null) throw e; InetAddress ipAddress = networkInterface.getInetAddresses().nextElement(); if (ipAddress == null) throw e; return ipAddress.getHostAddress(); } } }