public static InetAddress getLocalIpAddress ( ) throws RuntimeException { try { List < InetAddress > ips = getLocalIpAddresses ( false , true ) ; for ( InetAddress ip : ips ) { log . debug ( "[IpHelper] {getLocalIpAddress} Considering locality of " + ip . getHostAddress ( ) ) ; if ( ! ip . isAnyLocalAddress ( ) && ( ip instanceof Inet4Address ) ) { // Ubuntu sets the unix hostname to resolve to 127.0.1.1; this is annoying so treat it as a loopback if ( ! ip . getHostAddress ( ) . startsWith ( "127.0." ) ) { log . debug ( "[IpHelper] {getLocalIpAddress} Found nonloopback IP: " + ip . getHostAddress ( ) ) ; return ip ; } } } log . trace ( "[IpHelper] {getLocalIpAddress} Couldn't find a public IP in the ip (size " + ips . size ( ) + ")" ) ; return InetAddress . getLocalHost ( ) ; } catch ( UnknownHostException e ) { throw new RuntimeException ( "[FileHelper] {getLocalIp}: Unable to acquire the current machine's InetAddress" , e ) ; } }
Returns the primary InetAddress of localhost