public class LocalAddressUtils extends Object
LocalAddressUtils.warmUp()during the application's startup phase so that results are readily available when needed.
The main method is
The other methods essentially exist to provide input to this method but are
exposed nevertheless if anyone wants them.
These utility methods are in particular relevant in relation to
the PAC helper method
myIpAddress(). However, the class may indeed
be used for any use case.
Note, that there is no single correct answer to the question about determining the local host's IP address in an environment with multiple network interfaces or multiple addresses on each network interface.
|Modifier and Type||Method and Description|
Returns the address of the local host.
Returns the addresses of the local host.
Returns the loopback address.
Returns the host's IP address.
Returns the host's IP addresses.
Returns a prioritized list of local host addresses.
Tries to guess if the network interface is a virtual adapter by one of the makers of virtualization solutions (e.g.
Refreshes the cached network information.
Starts collecting network information in a background task.
public static void warmUp()
public static void refreshNetworkInfo(boolean await)
await- if true, waits for the refresh to complete
@NonNull public static InetAddress getLocalHost() throws UnknownHostException
This method returns a cached result of calling
InetAddress.getLocalHost() and is therefore likely not to
block (unlike the underlying method) unless this is the first time
this class is being referenced.
InetAddress#getLocalHost() is known to return
unpredictable results for hosts with multiple network adapters. The
method is much more likely to return an acceptable result.
@NonNull public static InetAddress getLocalHostAddresses(IpAddressUtils.IpTypePreference ipTypePref) throws UnknownHostException
This is achieved by retrieving the
from the system, then resolving that name into a list of
This method returns a cached result and is therefore likely not to block unless this is the first time this class is being referenced.
@NonNull public static List<InetAddress> getPrioritizedLocalHostAddresses(IpAddressUtils.IpTypePreference ipTypePref)
Prioritization is done on the following basis:
look like a software virtual adaptersare prioritized lower than addresses belonging to interfaces which don't look software virtual adapters.
The method returns a cached result and is therefore likely not to block, unless this is the first time this class is being referenced.
@NonNull public static InetAddress getMostLikelyLocalInetAddresses(IpAddressUtils.IpTypePreference ipTypePref)
InetAddress.getLocalHost(), in particular on hosts with multiple network interfaces or hosts that are virtualized or operating in a PaaS environment.
The method uses the following prioritization for determining what to return, by continously moving to the next step if the previous step yielded an empty result:
getLocalHostAddresses()(List A) is compared to
getPrioritizedLocalHostAddresses()(List B), picking the ones from List B list which is also on List A.
LocalAddressUtils.getLocalHost()if it matches the
The method uses the other methods in the class and is therefore likely not to block, unless this is the first time this class is being referenced.
ipTypePref- IP protocol filter
@NonNull public static InetAddress getMostLikelyLocalInetAddress(IpAddressUtils.IpTypePreference ipTypePref)
getMostLikelyLocalInetAddresses()but only returns a single IP address.
ipTypePref- IP protocol filter
@NonNull public static InetAddress getLoopbackAddress(IpAddressUtils.IpTypePreference ipTypePref)
This method is similar to
except that the preference for IPv4 vs IPv6 can be explicitly
For IPv4 the returned address is always
127.0.0.1 and for
IPv6 it is
ipTypePref- IPv4 vs IP4v6 preference
public static boolean isSoftwareVirtualAdapter(NetworkInterface nif)
nif- network interface