Java Utililty Methods Local Host Get

List of utility methods to do Local Host Get

Description

The list of methods to do Local Host Get are organized into topic(s).

Method

StringgetLocalHost()
Returns the non-localhost IP address of the current machine.
String address = null;
try {
    address = InetAddress.getLocalHost().getHostAddress();
    if (address.equals(LOCAL_HOST)) {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        boolean found = false;
        while (interfaces.hasMoreElements() && !found) {
            NetworkInterface e = interfaces.nextElement();
...
InetAddressgetLocalHost()
get Local Host
try {
    return (InetAddress.getLocalHost());
} catch (Throwable e) {
    try {
        Enumeration nis = NetworkInterface.getNetworkInterfaces();
        while (nis.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) nis.nextElement();
            Enumeration addresses = ni.getInetAddresses();
...
InetAddressgetLocalHost()
get Local Host
try {
    return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
    throw new AssertionError(e);
StringgetLocalHost()
get Local Host
try {
    return InetAddress.getLocalHost().getCanonicalHostName();
} catch (final UnknownHostException uhe) {
    final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        final NetworkInterface networkInterface = interfaces.nextElement();
        if (null != networkInterface) {
            final Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
...
InetAddressgetLocalHost()
Use this instead of InetAddress#getLocalHost() to prevent multiple lookups.
if (localhost == null) {
    try {
        localhost = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
return localhost;
...
InetAddressgetLocalHost()
Methods returns InetAddress for localhost
InetAddress addr;
try {
    addr = InetAddress.getLocalHost();
} catch (ArrayIndexOutOfBoundsException e) { 
    addr = InetAddress.getByName(null);
return addr;
InetAddressgetLocalHost()
get Local Host
try {
    return InetAddress.getLocalHost();
} catch (UnknownHostException e) {
    return getLocalHostByName();
InetAddressgetLocalHost()
Smarter way to get localhost than InetAddress.getLocalHost.
String osName = System.getProperty("os.name");
if (osName != null && osName.toLowerCase().contains("windows")) {
    return InetAddress.getLocalHost();
InetAddress loopback = null;
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
    NetworkInterface i = e.nextElement();
...
InetAddressgetLocalHost()
Returns an InetAddress representing the address of the localhost.
InetAddress localHost = InetAddress.getLocalHost();
if (!localHost.isLoopbackAddress()) {
    return localHost;
InetAddress[] addrs = getAllLocalUsingNetworkInterface();
for (InetAddress addr : addrs) {
    if (!addr.isLoopbackAddress() && !addr.getHostAddress().contains(":")) {
        return addr;
...
InetSocketAddressgetLocalHost()
get Local Host
try {
    return new InetSocketAddress(InetAddress.getByName("192.168.0.10"), 0);
} catch (final UnknownHostException e) {
return null;