5

I want to push a file from my Java program to an Android emulator. Now, I can launch the emulator by using ProcessBuilder and also trap the logcat messages. But whenever I'm trying to use the adb push command in process builder, the process hangs and no output is generated.

The code:

try {
    ProcessBuilder proc = new ProcessBuilder("D://android-sdk//platform-tools//adb.exe",
                                             "push D:\\final.xml /mnt/sdcard/final.xml");
    Process p = proc.start();
    BufferedReader br2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ( (line = br2.readLine()) != null)
        System.out.println(line);
} catch (Exception e) {
    System.err.println("Error");
}

EDIT:- Found the probabble solution. I was using Process.waitFor() method but not storing its returned exitcode. Now as i did this:

int exitVal = p.waitFor();

Everything worked as a charm.

And @Marc Van Daele Thanks for your input. as per my experience, ProcessBuilder works in both ways ie. You can use arguments separated by spaces or by commas. :)

user1471910
  • 67
  • 1
  • 7
  • To be honest, I think it's unlikely that storing the return value of waitFor solves the issue. I rather think that, sometimes, you are fast enough to read the data out of the InputStream (and then it works OK) and sometimes you are not, and then you run into the 'hang' issue. – Marc Van Daele Jun 21 '12 at 12:44
  • Yeah.. after I made the edit .. it felt kinda wierd to me too. But I am not facing the same issue for quite some time now. Thanks for your help guyz :) – user1471910 Jun 22 '12 at 14:25

1 Answers1

5

Shouldn't this be separate arguments like

ProcessBuilder proc = new ProcessBuilder("D://android-sdk//platform-tools//adb.exe", "push",  "D:\\final.xml", "/mnt/sdcard/final.xml");
Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52
  • you are reading the p.getInputStream()? I think you should read the OutputStream and the ErrorStream. Probably, it's also a good idea to set proc.redirectErrorStream(true); and only read the OutputStream. Probably you are running into "Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock." as described http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html – Marc Van Daele Jun 21 '12 at 11:47
  • ignore my (incorrect) comment on input vs out stream above. The remainder of my comment is valid though. – Marc Van Daele Jun 21 '12 at 11:55
  • I actually made some experiments and this answer seems to be right. – Andrey Ermakov Jun 21 '12 at 12:12
  • Amazing ! great answer.. saved lots of time.. Thanks a lot! – Danger Dec 07 '17 at 06:25