Here you can find the source of getLocalhostIp()
public static String getLocalhostIp()
//package com.java2s; //License from project: Open Source License import java.net.*; import java.util.Enumeration; public class Main { public static String getLocalhostIp() { try {//w ww .ja v a2 s . co m for (final Enumeration<NetworkInterface> interfaces = NetworkInterface .getNetworkInterfaces(); interfaces.hasMoreElements();) { final NetworkInterface cur = interfaces.nextElement(); if (cur.isLoopback()) { continue; } if (cur.getName().startsWith("vnic")) { // skip parallels virtual interfaces continue; } for (final InterfaceAddress interfaceAddress : cur.getInterfaceAddresses()) { final InetAddress inetAddress = interfaceAddress.getAddress(); if (!((inetAddress instanceof Inet4Address))) { continue; } return inetAddress.getHostAddress(); } } return "localhost"; } catch (Throwable e) { return "localhost"; } } }