code: public static String getServerIPv4() {
          String candidateAddress = null;
          try {
              Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
              while (nics.hasMoreElements()) {
                  NetworkInterface nic = nics.nextElement();
                  Enumeration<InetAddress> inetAddresses = nic.getInetAddresses();
                  while (inetAddresses.hasMoreElements()) {
                      String address = inetAddresses.nextElement().getHostAddress();
                      String nicName = nic.getName();
                      if (nicName.startsWith("eth0") || nicName.startsWith("en0")) {
                          return address;
                      }
                      if (nicName.endsWith("0") || candidateAddress == null) {
                          candidateAddress = address;
                      }
                  }
              }
          } catch (SocketException e) {
              throw new RuntimeException("Cannot resolve local network address", e);
          }
          return candidateAddress == null ? "127.0.0.1" : candidateAddress;
      }
docs: Returns server IP address (v4 or v6) bound to local NIC. If multiple NICs are present, choose 'eth0' or 'en0' or
  any one with name ending with 0. If non found, take first on the list that is not localhost. If no NIC
  present (relevant only for desktop deployments), return loopback address.