0

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 Answer 1

3

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() } 
Sign up to request clarification or add additional context in comments.

3 Comments

this is the way I do it too
FWIW, executing external commands in java is usually an anti-pattern. There are plenty of libraries which can do HTTP transfers in Java.
@GyroGearless you mean for HTTP? That I can agree with. Not every external command is an anti-pattern.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.