Here you can find the source of getLocalHost()
static String getLocalHost() throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; import java.util.Enumeration; public class Main { static String getLocalHost() throws IOException { try {/*www.j a v a2 s .c om*/ return InetAddress.getLocalHost().getCanonicalHostName(); } catch (final UnknownHostException uhe) { final Enumeration<NetworkInterface> interfaces = NetworkInterface .getNetworkInterfaces(); while (interfaces.hasMoreElements()) { final NetworkInterface networkInterface = interfaces .nextElement(); if (null != networkInterface) { final Enumeration<InetAddress> addresses = networkInterface .getInetAddresses(); if (addresses.hasMoreElements()) { return addresses.nextElement().getHostAddress(); } } } throw uhe; } } }