public static Set < String > getLocalAddresses ( ) { try { List < NetworkInterface > interfaces = Collections . list ( NetworkInterface . getNetworkInterfaces ( ) ) ; Set < String > result = new HashSet <> ( ) ; for ( NetworkInterface networkInterface : interfaces ) { if ( networkInterface . isLoopback ( ) || ! networkInterface . isUp ( ) ) continue ; for ( InterfaceAddress address : networkInterface . getInterfaceAddresses ( ) ) { String addr = address . getAddress ( ) . getHostAddress ( ) ; if ( addr == null || addr . isEmpty ( ) || addr . contains ( ":" ) ) continue ; result . add ( addr ) ; } } return result ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }
This method returns set of local IP addresses available in system .