Vertigo Vertigo - 1 year ago 112
Android Question

Process hangs waitFor() method

I am trying to get all files and folders from /data/data folder in rooted android device from java. I have command that works correctly in windows cmd:

adb -s <device name> shell su -c ls /data/data/


I try to execute this command in java, everything seems ok but the line


never returns.

public ArrayList<String> execute(String command) {
ArrayList<String> line = new ArrayList<String>();
try {
ProcessBuilder pb = new ProcessBuilder(adbPath + "adb.exe ", command);
Process p = pb.start();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String tmp;
while ((tmp = in.readLine()) != null) {
line.removeAll(Arrays.asList("", null));
catch (Exception ex) {
return line;
//adbPath = C:\Users\redra_000\AppData\Local\Android\sdk\platform-tools\
//command = -s E7AZCY935671 shell su -c ls /data/data/

What am i doing wrong?

Answer Source

The javadoc says that the argument to the ProcessBuilder constructor is:

"... a string array containing the program and its arguments".

and the example makes it clear that it means separate strings for each argument. You have passed all of the arguments as one String. In addition, you have added a spurious space to the end of the command pathname.

I suggest that you look at the example in the javadoc to see how you are supposed to instantiate a ProcessBuilder object.

Another problem is that you appear to be calling waitFor() before you read the output from adb. If you do that and adb produces more output than can be buffered in the pipe, then you will get a deadlock. Call waitFor() after you have read all of the output.