Here you can find the source of getLocalIp()
public static String getLocalIp()
//package com.java2s; import java.net.Inet4Address; import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class Main { public static String getLocalIp() { try {/*from w w w . j av a2 s. c o m*/ for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) { NetworkInterface item = e.nextElement(); for (InterfaceAddress address : item.getInterfaceAddresses()) { if (address.getAddress() instanceof Inet4Address) { Inet4Address inet4Address = (Inet4Address) address.getAddress(); if (inet4Address.isLoopbackAddress()) { continue; } System.out.println(inet4Address.getHostAddress()); return inet4Address.getHostAddress(); } } } } catch (Exception e) { throw new IllegalStateException("no ip"); } throw new IllegalStateException("no ip"); } }