how to run Run a linux command(curl) through java and to get the xml response

Viewed 287

I want to execute a Linux command (curl) using java code

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ExecuteShellCommand {

    public 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()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

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

        return output.toString();

    }

    public static void main(String args[]) {

        ExecuteShellCommand com = new ExecuteShellCommand();


            System.out.println(com.executeCommand(
                    "curl -u '<username><pw>' -k <host>/services/search/jobs -d search=\"abc""));

        System.out.println("hello"); 

The console output is printing hello only with no errors though when i tried the command in git bash it will give a xml response.

0 Answers
Related