5

I am executing a curl command using java.

curl -i --user "OAMADMIN_tenant_358922247351079_svc_358922247369079_APPID:Iuj.2swilg5fhv" -H "Content-Type: application/json" -H "Accept: application/json" -H 'X-USER-IDENTITY-DOMAIN-NAME: tenant_358922247351079' -H "X-RESOURCE-IDENTITY-DOMAIN-NAME: tenant_358922247351079" --request GET "https://slc04yre-1.dev.oraclecorp.com:4443/oam/services/rest/11.1.2.0.0/oauth/admin/Clients?name=myMCS_svc_358922247369079_MCS_Client_OAUTHCLIENT"

I want to get the output of this curl command in my code,but my stdoutput is coming out to be empty.

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

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            //p.waitFor();
            String line = "";
            while ((line = reader.readLine()) != null) {
                System.out.println("line="+line);
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }

Tried executing the curl command manually, its working fine. Then I printed the standard error, and I can see:

   [testng] error line=
   [testng] error line=curl: (6) Couldn't resolve host 'application'
   [testng] error line=
   [testng] error line=curl: (6) Couldn't resolve host 'application'
   [testng] error line=
   [testng] error line=curl: (6) Couldn't resolve host 'tenant_359516638431079''
   [testng] error line=
   [testng] error line=curl: (6) Couldn't resolve host 'tenant_359516638431079"'
   [testng] error line=
   [testng] error line=curl: (1) Unsupported protocol: "https

When curl command is executed manually, its working fine then why not through Runtime.getRuntime() ?

Kindly suggest!! Any help will be appreciated.

3
  • I am building my command like this: String cmd = "curl -i --user \"" + appId + ":" + appIdPwd + "\" -H \"Content-Type: application/json\" -H \"Accept: application/json\" " + "-H 'X-USER-IDENTITY-DOMAIN-NAME: " + tenantName + "' -H \"X-RESOURCE-IDENTITY-DOMAIN-NAME: " + tenantName + "\" --request GET \"" + url.substring(0, url.lastIndexOf("/")) + "/oam/services/rest/11.1.2.0.0/oauth/admin/Clients?name=" + client + "\""; Commented May 18, 2015 at 11:00
  • Why do you execute external curl process? You can easily send and handle any http request directly in java. It would be much more efficient! See e.g. : mkyong.com/java/how-to-send-http-request-getpost-in-java Commented May 19, 2015 at 5:38
  • Another example with https request is here: stackoverflow.com/questions/6927427/… Commented May 19, 2015 at 5:42

1 Answer 1

5

It seems like the data shell/console is interpreting/changing characters. For example the following line:

-H "Content-Type: application/json"

... seems to be getting interpreted as three different arguments:

-H Content-Type: and application and /json by the shell/console.

Try breaking the command string down into an array of components using the format:

exec(String[] cmdarray)

That way it will be clear to the shell/console which arguments are grouped together.

Here is a test in groovy that proves the point:

def Object executeCommand(command) {

   def proc = Runtime.getRuntime().exec(command);
   def sout = new StringBuffer()
   def serr = new StringBuffer()

   proc.consumeProcessOutput(sout, serr)
   proc.waitFor()

   return [ 'sout':sout.toString(), 'serr':serr.toString() ]
}   

response = executeCommand('''curl --silent --show-error -H "Accept: application/json" --request GET "https://education.cloudant.com/"''')
assert response['sout'] == ''
assert response['serr'].startsWith( 'curl: (6) Could not resolve host: application' )

response = executeCommand(['curl', '--silent',  '--show-error',  '-H', 'Accept: application/json',  '--request', 'GET', 'https://education.cloudant.com/'] as String[] )
assert response['sout'].startsWith('{"couchdb":"Welcome","version":"1.0.2","cloudant_build":"2367"}')
assert response['serr'] == ''
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Chris. I tried breaking the curl command into an array and it worked. Thanks!!
@Sammi Can you provide the code which worked. I'm having a similar problem.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.