Here you can find the source of getIPFromInterface(String ni)
public static String getIPFromInterface(String ni) throws UnknownHostException, SocketException
//package com.java2s; //License from project: LGPL import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class Main { public static String getIPFromInterface(String ni) throws UnknownHostException, SocketException { if (ni.equals("lo")) { return InetAddress.getLocalHost().getHostAddress(); }/* ww w. j a v a 2 s .c om*/ //other Network interfaces Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces(); for (; n.hasMoreElements();) { NetworkInterface e = n.nextElement(); if (e.getDisplayName().equals(ni)) { return getIPAddress(e); } } return null; } public static String getIPAddress(NetworkInterface e) { Enumeration<InetAddress> a = e.getInetAddresses(); for (; a.hasMoreElements();) { InetAddress addr = a.nextElement(); //IPv6 addr = a.nextElement(); //IPv4 return addr.getHostAddress(); } return null; } }