code: public static synchronized String getLocalIpAddress(int timeoutMs) {
      if (sLocalIP != null) {
        return sLocalIP;
      }
  
      try {
        InetAddress address = InetAddress.getLocalHost();
        LOG.debug("address: {} isLoopbackAddress: {}, with host {} {}", address,
            address.isLoopbackAddress(), address.getHostAddress(), address.getHostName());
  
        // Make sure that the address is actually reachable since in some network configurations
        // it is possible for the InetAddress.getLocalHost() call to return a non-reachable
        // address e.g. a broadcast address
        if (!isValidAddress(address, timeoutMs)) {
          Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
  
          // Make getNetworkInterfaces have the same order of network interfaces as listed on
          // unix-like systems. This optimization can help avoid to get some special addresses, such
          // as loopback address"127.0.0.1", virtual bridge address "192.168.122.1" as far as
          // possible.
          if (!WINDOWS) {
            List<NetworkInterface> netIFs = Collections.list(networkInterfaces);
            Collections.reverse(netIFs);
            networkInterfaces = Collections.enumeration(netIFs);
          }
  
          while (networkInterfaces.hasMoreElements()) {
            NetworkInterface ni = networkInterfaces.nextElement();
            Enumeration<InetAddress> addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
              address = addresses.nextElement();
  
              // Address must not be link local or loopback. And it must be reachable
              if (isValidAddress(address, timeoutMs)) {
                sLocalIP = address.getHostAddress();
                return sLocalIP;
              }
            }
          }
  
          LOG.warn("Your hostname, {} resolves to a loopback/non-reachable address: {}, "
                    +  "but we couldn't find any external IP address!",
                    InetAddress.getLocalHost().getHostName(), address.getHostAddress());
        }
  
        sLocalIP = address.getHostAddress();
        return sLocalIP;
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
docs: Gets a local IP address for the host this JVM is running on.
  
  @param timeoutMs Timeout in milliseconds to use for checking that a possible local IP is
  reachable
  @return the local ip address, which is not a loopback address and is reachable