How to use curl in java and parse json response


I have a curl command that gives a JSON response. I want to use this curl command in java & parse the JSON response in java. Is it possible to do so??

EDIT: When I do the following: public static void main(String[] args) throws Throwable {

try { InputStream stream = Runtime.getRuntime().exec("curl --globoff --insecure --silent -u username:password -X GET -H 'Content-Type: application/json' \"http://ficcjira.xyz.com/rest/api/2/search?jql=project=ABC&fields=Timetracking\"").getInputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(stream), 1); input.readLine(); input.read(); input.read(); DataReader reader = new JsonReader(input) .addField("TimeSpent", "//array/object/timespent") .addRecordBreak("//array/object"); /*reader = new TransformingReader(reader) .add(new BasicFieldTransformer("TimeSpent").stringToDouble());*/ DataWriter writer = new StreamWriter(System.out); JobTemplate.DEFAULT.transfer(reader, writer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

I get the following error: Cannot run program "curl": CreateProcess error=2, The system cannot find the file specified


You can do, and read the stream using some json parser, for example Gson:

try ( Reader reader = new InputStreamReader( Runtime.getRuntime().exec("curl -h").getInputStream() ) ) { JsonElement element = new JsonParser().parse(reader); }


This is a horrible way to access the Jira Rest api. Since it is a REST API, you can call it in many ways. Some of them are:

<ol><li>Rest Java Client for Java. <a href="https://marketplace.atlassian.com/plugins/com.atlassian.jira.jira-rest-java-client/server/overview" rel="nofollow">https://marketplace.atlassian.com/plugins/com.atlassian.jira.jira-rest-java-client/server/overview</a> </li> </ol>

I have some sample code for you here: <a href="https://github.com/somaiah/jrjc" rel="nofollow">https://github.com/somaiah/jrjc</a>

<ol start="2"><li>

Jersey: Use Jersey to call the REST APIs and read the responses.

</li> <li>

If you are using Spring, you can use the RestTemplate. I have a small sample here: <a href="https://github.com/somaiah/restTemplate" rel="nofollow">https://github.com/somaiah/restTemplate</a>

</li> </ol>

Of course there is a whole slew of options for you- Restlet, Apache commons and so on.

You should use the curl option only if you are calling this from a bash script. In that case you can parse the response with jq (which I love) <a href="https://stedolan.github.io/jq/" rel="nofollow">https://stedolan.github.io/jq/</a>


Runtime rt = Runtime.getRuntime(); Process p1; Process p2; try { String postSubmissionJSon = "{"data":"value"}"; String[] stringPost = {"curl", "-X", "POST", "https://your-url.com/api/posts", "--cert", "test.crt.pem", "--key", "test.key.pem", "-H", "accept: application/json", "-H", "content-type: application/json", "-d", postSubmissionJSon}; ProcessBuilder ps = new ProcessBuilder(stringPost); //ps.redirectErrorStream(true); Process pr = ps.start(); pr.waitFor(); BufferedReader reader2 = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line2 = ""; while ((line2 = reader2.readLine()) != null) { output.append(line2 + "\n"); } System.out.println("\n\n\noutput====" + output + "===\n\n\n"); String sbToString = output.toString(); jSONObject = new JSONObject(sbToString); System.out.println(jSONObject.toString()); p1 = Runtime.getRuntime().exec("pwd"); p1.waitFor(); BufferedReader reader1a = new BufferedReader(new InputStreamReader(p1.getInputStream())); String line1a = ""; while ((line1a = reader1a.readLine()) != null) { output.append(line1a + "\n"); System.out.println("output====" + output + "==="); } } catch (Exception e) { System.out.println("===============ERROR===============\n" + e.getMessage() + "\n\n\n"); }

/*NOTE : Your Key (test.key.pem) and Cert (test.crt.pem) should be located on your proper directory. For glassfish it is on "~/glassfish4/glassfish/domains/domain1/config" or your can specify exact location by changing "--cert", "test.crt.pem" --> "/home/user/files/test.crt.pem". Same with the key. */


  • c++ byte array to bitmapimage
  • Java Generic object initialization
  • DES3 encryption: ruby openssl::cipher vs. oracle dbms_obfuscation_toolkit
  • How to send data between .NET 4.5 and .NET 4.0 with Sockets?
  • Signing PDF - memory consumption
  • Multithreaded Server receives data from just one client JAVA
  • Servlets 3.1: how to handle body in DELETE request
  • How to avoid OutOfMemoryException while loading large chunks of data from a database?
  • The process cannot access the file because it is being used by another process
  • Problems with show dialog on Background task in windows phone 8.1
  • How to add header line to CSV without loading whole file?
  • Android: save file to downloads that can be viewed later
  • networkstream “cannot access a disposed object” when using newly created networkstream
  • LocateRegistry.createRegistry
  • Searching Persian characters and words in SQL server with different encoding
  • Complicated COUNT query in MySQL
  • Hadoop (java) change the type of Mapper output values
  • Error when trying to send an email using Gmail API in Java
  • 302 Redirect from http to https in Android using Dropbox short Hyperlinks
  • Apache Lucene - Improving the results of Spell Checker
  • Extracting a process's exit code in the case of ThreadInterrupted
  • problem with classes not found during PlayPlugin.enhance
  • Runtime error in UVA Online Judge [closed]
  • 3.0.0.M1: SSL - Invalid keystore format
  • Fragment gives me an error while inflating 1
  • Correct implementation of List Iterator methods
  • How to copy styled text in JTextPane
  • Hibernate to update table schema
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Jackson Parser: ignore deserializing for type mismatch
  • How to test if a URL from an Eclipse bundle is a directory?
  • Change multiple background-images with jQuery
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • Trying to switch camera back to front but getting exception
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • GridView Sorting works once only
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How does Linux kernel interrupt the application?