NetworkUtil { public static InetAddress findLocalAddressViaNetworkInterface() { Enumeration<NetworkInterface> networkInterfaces; try { networkInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { return null; } while (networkInterfaces.hasMoreElements()) { NetworkInterface nif = networkInterfaces.nextElement(); for (Enumeration<InetAddress> addrEnum = nif.getInetAddresses(); addrEnum.hasMoreElements(); ) { InetAddress interfaceAddress = addrEnum.nextElement(); if (useInetAddress(nif, interfaceAddress)) { return interfaceAddress; } } } return null; } private  NetworkUtil(); static void main(String[] args); static InetAddress getLocalAddress(); static InetAddress getLocalAddressWithMulticast(); static InetAddress findLocalAddressViaNetworkInterface(); static boolean isMulticastSupported(); static boolean isMulticastSupported(NetworkInterface pNif); static boolean isMulticastSupported(InetAddress pAddr); static List<InetAddress> getMulticastAddresses(); static String getAgentId(int objectId, String type); static String sanitizeLocalUrl(String pRequestURL); static String dumpLocalNetworkInfo(); static String replaceExpression(String pValue);  }
@Test public void findLocalAddress() throws SocketException { Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); System.out.println("IFs"); boolean found = false; while (ifs.hasMoreElements()) { NetworkInterface intf = ifs.nextElement(); System.out.println(intf + " is loopback: " + intf.isLoopback()); found = found || (!intf.isLoopback() && intf.supportsMulticast() && intf.isUp()); } InetAddress addr = NetworkUtil.findLocalAddressViaNetworkInterface(); System.out.println("Address found via NIF: " + addr); assertTrue(found ? addr != null : addr == null); if (addr != null) { assertTrue(addr instanceof Inet4Address); } }