0

Possible Duplicate:
How to use javas Process.waitFor()?

I have a program to upload to Arduino in my pc. I created a makefile for compiling and uploading it to arduino fio.

It works fine. Now I am trying to create a java program for executing that makefile but I have problems with this order:

Runtime.getRuntime().exec("make");

When I execute it, program gets blocked.

Is there anything I should do to execute that order? With other orders, it works fine. make clean, make depend....

I am trying with this code I found on internet for that task:

public static void prueba() throws InterruptedException, IOException {

    Process proc = Runtime.getRuntime().exec("make");
    int size;
    String s;
    int exCode = proc.waitFor();
    StringBuffer ret = new StringBuffer();



    while((size = proc.getInputStream().available()) != 0) {
        byte[] b = new byte[size];
        proc.getInputStream().read(b);
        s = new String(b);
        ret.append(s);
    }
    System.out.println(ret.toString());         
}
Community
  • 1
  • 1
Biribu
  • 535
  • 3
  • 12
  • 27

1 Answers1

1

You need to read both the input stream (stdout) and the error stream (stderr) in separate threads before calling process.waitFor(), in order to prevent blocking.

This article is a must-read: "When Runtime.exec() won't" and shows you how to use Runtime.exec properly.

dogbane
  • 266,786
  • 75
  • 396
  • 414