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. :)

2
  • 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.CommentedJun 21, 2012 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 :)CommentedJun 22, 2012 at 14:25

1 Answer 1

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"); 
4
  • 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 docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.htmlCommentedJun 21, 2012 at 11:47
  • ignore my (incorrect) comment on input vs out stream above. The remainder of my comment is valid though.CommentedJun 21, 2012 at 11:55
  • I actually made some experiments and this answer seems to be right.CommentedJun 21, 2012 at 12:12
  • Amazing ! great answer.. saved lots of time.. Thanks a lot!
    – Danger
    CommentedDec 7, 2017 at 6:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.