Running Command line commands within Java

Viewed 40

Is it possible to run this cmd line command curl ipinfo.io within java?

I want to then pull the lat and long or "loc" from it afterwards. Thanks

1 Answers

You can use

var p = Runtime.getRuntime().exec("curl ipinfo.io")

And read the output like:

  try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
                return bufferedReader.lines().collect(Collectors.toList());
            } catch (IOException e) {
              e.printStackTrace()
            }
Related