Here you can find the source of getLocalIP()
public static String getLocalIP() throws Exception
//package com.java2s; //License from project: Apache License import java.net.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class Main { public static String EXCLUDE_IP = "10."; public static String NET_INTERFACE; public static List<String> getLocalIP(String nic, String exclude) throws Exception { List<String> result = new ArrayList<String>(); NetworkInterface ni;//from w ww . ja va2 s. c o m Enumeration<InetAddress> ias; InetAddress address; String ip; Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { ni = netInterfaces.nextElement(); if (nic != null && !nic.isEmpty() && !ni.getName().equals(nic)) { continue; } ias = ni.getInetAddresses(); while (ias.hasMoreElements()) { address = ias.nextElement(); if (!address.isLoopbackAddress() && (address instanceof Inet4Address)) { ip = address.getHostAddress(); result.add(ip); } } } int count = result.size(); if (count <= 1) { return result; } if (exclude != null && !exclude.isEmpty()) { for (int i = count - 1; i >= 0; i--) { ip = result.get(i); if (ip.startsWith(exclude)) { result.remove(i); count--; if (count == 1) { break; } } } } return result; } public static String getLocalIP(final String exclude) throws Exception { List<String> ips = getLocalIP(NET_INTERFACE, exclude); if (ips != null && !ips.isEmpty()) { if (ips.size() == 1) { return ips.get(0); } for (String ip : ips) { if (ip.startsWith("172.")) { return ip; } else if (ip.startsWith("192.")) { return ip; } } return ips.get(0); } return null; } public static String getLocalIP() throws Exception { return getLocalIP(EXCLUDE_IP); } }