3

I am trying to run some Java to run cURL to hit a website with data and this is working.

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("curl -s -S http://foo.com/testcode/Moo.cfm?PageName=" + $(Page.Name) + "&ProjectName=" + $(ProjectName));

cURL is hitting this website and sending the PageName and ProjectName variables.

The web page outputs some JSON.

How do i capture this JSON that the website is displaying and use it in my Java?

Chad Gray
  • 73
  • 1
  • 6
  • It might be easier to use a Java HTTP client such as `HttpClient` – Steve Kuo Jun 22 '17 at 20:14
  • Oh... i will have to google that. I don't know Java very well as you can tell. Thanks! I will see how HttpClient works! – Chad Gray Jun 22 '17 at 20:21
  • Or the `Http[s]URLConnection` classes built-in to Java. Once you get JSON text by whichever method you probably want a JSON parser; https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java has 27 answers, some highly voted. – dave_thompson_085 Jun 22 '17 at 21:24

2 Answers2

2

You need to read the input stream from the process. Something like this should work using Java 8

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("curl -s -S http://foo.com/testcode/Moo.cfm?PageName=" + $(Page.Name) + "&ProjectName=" + $(ProjectName));

//Java 8 version
String result = new BufferedReader(
                           new InputStreamReader(pr.getInputStream()))
                               .lines()
                               .collect(Collectors.joining("\n"));


//Older version than java 8
BufferedReader response = new BufferedReader(new InputStreamReader(pr.getInputStream()));
StringBuilder result = new StringBuilder();
String s;
while((s = response.readLine()) != null) {
    result.append(s);
}
System.out.println(result.toString());

//You then need to close the BufferedReader if not using Java 8
response.close();

This will then print the full result in both cases

However, if you are looking for ways to just talking to websites you are probably better off using something like HttpClient or OKHttp.

If you are retrieving JSON I would suggest something like like Google GSON to parse it into objects. There is a good post on how to parse JSON at How to parse JSON in Java

Asthor
  • 598
  • 4
  • 17
  • Thanks for the input. Sorry i cannot figure out how to post code in a comment on this website. I ended up with this, but i will need to keep working on it to parse the JSON. `code` Runtime rt = Runtime.getRuntime(); Process pr = rt.exec("curl -s -S http://signetprojects.careyweb.com/testcode/ClariFai.cfm?PageName=" + $(Page.Name) + "&ProjectName=" + $(ProjectName)); BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream())); String input; input = br.readLine(); System.out.println(input); – Chad Gray Jun 23 '17 at 16:23
  • Should i be using .close() or .destroy() on the Process? I am getting some strange results and i wonder if these need to be managed and shut down after use? – Chad Gray Jun 23 '17 at 19:53
  • @ChadGray I update the post a bit. If you don't use the Java 8 way I posted you need to close the reader. I would suggest using the link I put on the bottom for reading JSON. There are some excellent answers there. – Asthor Jun 24 '17 at 19:38
  • Thanks! This works great now. I will be reading up on the JSON parsing you mention now that i have good communication. – Chad Gray Jun 25 '17 at 15:37
0

I guess i will post it as an answer, but this output the first line of text that the website returned. Now i need to learn how to parse the JSON. I am such a Java newbie... thanks for everyone help!

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("curl -s -S 
Process pr = rt.exec("curl -s -S http://foo.com/testcode/Moo.cfm?PageName=" + $(Page.Name) + "&ProjectName=" + $(ProjectName));

BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String response;

response = br.readLine();
System.out.println(response);
Chad Gray
  • 73
  • 1
  • 6