0

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??

With below Java code:

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
Om Sao
  • 7,064
  • 2
  • 47
  • 61
user3591433
  • 105
  • 1
  • 3
  • 11

4 Answers4

1
        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. */

0

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:

  1. Rest Java Client for Java. https://marketplace.atlassian.com/plugins/com.atlassian.jira.jira-rest-java-client/server/overview

I have some sample code for you here: https://github.com/somaiah/jrjc

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

  2. If you are using Spring, you can use the RestTemplate. I have a small sample here: https://github.com/somaiah/restTemplate

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) https://stedolan.github.io/jq/

Somaiah Kumbera
  • 7,063
  • 4
  • 43
  • 44
  • Testing your RestTemplate sample gives me the PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target error. How to fix it? – user3591433 Jun 16 '16 at 17:35
  • Guessing your jira domain is http, and you are trying to access it with https. If so, change your URL location. If not, you will need to add some certificates to your java keystore, like this: https://confluence.atlassian.com/kb/unable-to-connect-to-ssl-services-due-to-pkix-path-building-failed-779355358.html – Somaiah Kumbera Jun 16 '16 at 18:08
  • So, it is https. But I'm okay to make Java accept all certificates. – user3591433 Jun 16 '16 at 18:26
0

Going to answer based on the error you mentioned. You need to have cUrl installed and make sure it is added in your PATH variable.

To check, you can try to execute curl command from your Windows command prompt. If you are unable to see a valid response, you might not have cUrl nor not present in your PATH variable.

To install, find them on https://curl.haxx.se/download.html and just extract. Then add them in PATH variable and try running the cUrl command again.

-1

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);
}
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115