Here you can find the source of getLocalIP()
public static String getLocalIP() throws Exception
//package com.java2s; //License from project: Open Source License import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class Main { public static String getLocalIP() throws Exception { String localip = ""; InetAddress inetaddress = InetAddress.getLocalHost(); localip = inetaddress.getHostAddress(); if ("127.0.0.1".equals(localip) || "localhost".equals(localip) || localip.indexOf("local") != -1) { Enumeration<NetworkInterface> allnetinterface = NetworkInterface .getNetworkInterfaces(); while (allnetinterface.hasMoreElements()) { NetworkInterface net = allnetinterface.nextElement(); Enumeration<InetAddress> ipenum = net.getInetAddresses(); while (ipenum.hasMoreElements()) { InetAddress ip = ipenum.nextElement(); String t_ip = ip.getHostAddress(); if (!"127.0.0.1".equals(t_ip) && !"localhost".equals(t_ip) && !t_ip.contains(":")) { localip = t_ip;//from www .j ava2 s. c o m } } } } return localip; } }