Get Local IP Address Using Java


Java code example to read ip address from physical network address. Java lib code InetAddress.getLocalHost() should give you the IP address of machine. The problem is that a host could have more than one network interfaces, and an interface could be bound to more than one IP address. Some of the IP addresses could be virtual devices, and others could be private network IP addresses.

Here is java code example to read network ip address

public class IPAddressReader {
    static String localIpAddr = null;
    public static void main(String[] args) {
        System.out.println(getLocalAddress());
    }

    static void lookupLocalAddress() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            netInterFace : while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces
                        .nextElement();
                // Check for mac address
                byte[] hardwareAddress = networkInterface.getHardwareAddress();
                if ((hardwareAddress == null) || (hardwareAddress.length == 0)) {
                    continue;
                }

                Set<InetAddress> subAddresses = new HashSet<InetAddress>();
                Enumeration<NetworkInterface> subInterfaces = networkInterface
                        .getSubInterfaces();
                while (subInterfaces.hasMoreElements()) {
                    NetworkInterface subInterface = subInterfaces.nextElement();
                    for (InterfaceAddress interfaceAddress : subInterface
                            .getInterfaceAddresses()) {
                        subAddresses.add(interfaceAddress.getAddress());
                    }
                }

                List<InterfaceAddress> interfaceAddresses = networkInterface
                        .getInterfaceAddresses();
                for (InterfaceAddress interfaceAddress : interfaceAddresses) {
                    InetAddress address = interfaceAddress.getAddress();
                    if (address.isLoopbackAddress()) {
                        continue;
                    }
                    if (subAddresses.contains(address)) {
                        continue;
                    }
                    if (address instanceof Inet4Address) {
                        localIpAddr = address.getHostAddress();
                        System.out.println(localIpAddr);
                        break netInterFace;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (localIpAddr == null) {
            try {
                localIpAddr = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                localIpAddr = "127.0.0.1";
            }
        }
    }

    public static String getLocalAddress() {
        if (localIpAddr == null) {
            lookupLocalAddress();
        }
        return localIpAddr;
    }
}