2

I am trying to get the output of curl command in java.

I am able to execute the same curl command manually through terminal and obtain the output but when I try to execute through java code as below, the output obtained is null.

I could use Apache HttpClient for the same, but I want to try and use this curl cli.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class MyCurlClient {

  public static void main(String[] args) {
    MyCurlClient obj = new MyCurlClient();

    // in mac oxs
    // String command = "ping -c 5 " + domainName;
    String command = "curl "
        + "'http://localhost:8080/auth/login' -H 'Origin:   http://localhost:9000' --data-binary '{\"username\":\"policy-engine\",\"password\":\"openstack\"}' --compressed";

    String output = obj.executeCommand(command);
    System.out.println(output);
  }

  private String executeCommand(String command) {
    StringBuffer output = new StringBuffer();

    Process p;
    try {
      p = Runtime.getRuntime().exec(command);
      p.waitFor();
      BufferedReader reader = new BufferedReader(new InputStreamReader(
          p.getInputStream()));
      System.out.println(reader.readLine()); // value is NULL
      String line = "";
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
        output.append(line + "\n");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return output.toString();
  }
}

3 Answers 3

2

process.waitFor() waits until the process is terminated.

Therefore, you won't see any output from it. You need to capture the output before the process terminates.

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

You can fix your code by removing the process.waitFor() line.


You may want to try using an absolute path to the curl command, e.g.

command = "/usr/bin/curl " + ...

It's possible Java is not able to find the curl binary and that is why it doesn't work.


If this doesn't fix your problem, verify that the curl command is working at all by doing:

String[] command = new String[]{"curl", "http://localhost:8080/auth/login",
         "-H", "Origin:   http://localhost:9000", "--data-binary",
         "{\"username\":\"policy-engine\",\"password\":\"openstack\"}", "--compressed"};


private String executeCommand(String... command) {
  ProcessBuilder builder = new ProcessBuilder(command);
  builder.redirectOutput(new File("curloutput.txt"));
  p.start();
}

And see if the curl command actually outputs anything to the file. If it does, then this is an InputStream timing issue. If it does not, then something is wrong with the curl command itself.

3
  • Commenting p.waitFor() did not help. Still the value of reader.readLine() is NULL
    – anoop
    Aug 20, 2015 at 22:04
  • @anoop Let me know how either of those ideas work out
    – durron597
    Aug 20, 2015 at 22:13
  • Using String[] along with removing the single quotes from the command solved the issue.
    – anoop
    Aug 21, 2015 at 17:09
1

Using the String[] along with removing the single quotes from the curl command solved the issue.

Uf only String[] is used(with single quotes) then you get the expected response but along with it, you also see "unauthorized" as output.

Result is -->

{"username":"policy-engine","token":"dc7e017f-d5a3-4b72-a1c2-066880e775c7"}unauthorized

but when both String[] is used (without single quotes) the response is clean and as expected.

Result is -->

{"username":"policy-engine","token":"dc7e017f-d5a3-4b72-a1c2-066880e775c7"}
2
  • Why did you take my accept away? I helped you with your answer; I told you to use String[]; I put a lot of work into help you and taking the accept away costs me 15 reputation points and doesn't give yourself any points.
    – durron597
    Aug 22, 2015 at 22:10
  • Since i had consolidated the answer I accepted my own answer. din realize it affects ur reputation, am fairly new to SOF. You will get your accept back
    – anoop
    Aug 24, 2015 at 22:49
0

Please remove the quotes in th curl command , For Eg - String command = "curl http://localhost:8080/auth/login -H Origin: shttp://localhost:9000 --data-binary {\"username\":\"policy-engine\",\"password\":\"openstack\"} --compressed";

1
  • removing quotes along with using String[] helped solve the issue. thanks
    – anoop
    Aug 21, 2015 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.