RagHaven
RagHaven

Reputation: 4347

Java runtime.getruntime() gives no output

I am trying to execute a curl command as a Process through Java. When I execute it, I am not getting back any output at all. When I execute the same command through the command line I am getting the proper output. The URL returns the output as XML.

Here is the code for my Java program.

public static void exec(){
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"curl", "http://genome.ucsc.edu/cgi-bin/das/mm9/dna\\?segment\\=chr1:3206424,3206499"});
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=null;
            while((line=input.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Here is the command I am executing through the command line

curl http://genome.ucsc.edu/cgi-bin/das/mm9/dna\?segment\=chr1:3206424,3206499

When I run the Java program using the following String array {"ls", "-al"} it gives me the expected output

I would appreciate any help.

Upvotes: 1

Views: 1245

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201527

You aren't waiting for the Process to complete. Also, you could use ProcessBuilder and inheritIO() like

ProcessBuilder pb = new ProcessBuilder("curl",
        "http://genome.ucsc.edu/cgi-bin/das/mm9/dna?"
        + "segment=chr1:3206424,3206499");
pb.inheritIO();
try {
    Process p = pb.start();
    p.waitFor();
} catch (Exception e) {
    e.printStackTrace();
}

Edit

From your comment below, you'd need another Thread. Something like,

static class InputStreamReaderRunnable implements Runnable {
    private StringBuilder sb;
    private InputStream is;

    public InputStreamReaderRunnable(InputStream is) {
        this.is = is;
        this.sb = new StringBuilder();
    }

    @Override
    public void run() {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line).append(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return sb.toString();
    }
}

public static void main(String[] args) {
    ProcessBuilder pb = new ProcessBuilder("curl",
            "http://genome.ucsc.edu/cgi-bin/das/mm9/dna?segment=chr1:3206424,3206499");
    try {
        Process p = pb.start();
        InputStreamReaderRunnable isrr = new InputStreamReaderRunnable(
                p.getInputStream());
        Thread t = new Thread(isrr);
        t.start();
        p.waitFor();
        t.join();
        String out = isrr.toString();
        System.out.println(out);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 2

Related Questions