Java Adb Shell Returns Nothing
Solution 1:
The parameters for runtime.exec()
are wrong. You are using {'adb', '-s xxxx shell "ls /sdcard/"'}
when you should be using {'adb', '-s', 'xxxx', 'shell', 'ls /sdcard/'}
Solution 2:
Perhaps a bit too late for the OP, but could be useful for later viewers.
Recently I had to integrate one system to work closely with real devices via adb. Took a bit of time to figure out all the nasty bits of how to do it "properly". Then I had an idea to create a library that handles at least some basic adb commands (adb devices, shell, pull, push, install). It can do adb on local machine, or on remote machine over ssh (with key exchange setup) with the same API. Do not confuse with adb shell forwarding ;).
Its under apache 2.0 license: https://github.com/lesavsoftware/rem-adb-exec
Solution 3:
Thanks to Alex P. I found a solution that's working (need to parse the output, though, but that's a different story):
I just modified runCommand() to put the arguments in an String-array and give it to exec:
publicstaticStringrunCommand(Device d, String command) {
Runtime runtime = Runtime.getRuntime();
String full = "";
String error = "";
try {
String[] cmdWithDevice = newString[5];
String[] cmdWithoutDevice = newString[2];
if (d != null) {
cmdWithDevice[0] = ADB_LOCATION;
cmdWithDevice[1] = "-s";
cmdWithDevice[2] = d.getSerial();
cmdWithDevice[3] = "shell";
cmdWithDevice[4] = command;
command = "-s " + d.getSerial() + " " + command;
} else {
cmdWithoutDevice[0] = ADB_LOCATION;
cmdWithoutDevice[1] = command;
}
System.out.println("Running command: adb " + command);
// Process process = runtime.exec(new String[] {ADB_LOCATION, command});Process process = runtime.exec(cmdWithDevice[0] == null ? cmdWithoutDevice : cmdWithDevice);
BufferedReader stdin = newBufferedReader(newInputStreamReader(process.getInputStream()));
BufferedReader errin = newBufferedReader(newInputStreamReader(process.getErrorStream()));
process.waitFor();
String line;
while ((line = stdin.readLine()) != null) {
full += line;
}
while ((line = errin.readLine()) != null) {
error += line;
}
if (!error.isEmpty()) {
System.err.println("\n=== ADB reports: ===");
System.err.println(error);
}
stdin.close();
errin.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("App was overenthuasiastic. Couldn't wait for thread to finish.");
}
return full;
}
For some reason it does not work if I run runtime.exec manually with the arguments passed in, only programmatically.
This solution is a bit hacky, though and needs proper implementation. Also this solution assumes that every device-specific command is for shell.
Post a Comment for "Java Adb Shell Returns Nothing"