static String getLocalIPAddress ( ) { String ipAddress = "" ; try { List < NetworkInterface > netInterfaces = Collections . list ( NetworkInterface . getNetworkInterfaces ( ) ) ; for ( NetworkInterface netInterface : netInterfaces ) { List < InetAddress > addresses = Collections . list ( netInterface . getInetAddresses ( ) ) ; for ( InetAddress address : addresses ) { if ( ! address . isLoopbackAddress ( ) ) { String ip = address . getHostAddress ( ) ; boolean isIPv4 = ip . indexOf ( ' ' ) < 0 ; if ( isIPv4 ) { ipAddress = ip ; break ; } } } } } catch ( Throwable ignore ) { } return ipAddress ; }
Get IP address from first non local net Interface