Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute adb commands from java

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

like image 286
user1471910 Avatar asked Jun 21 '12 11:06

user1471910


People also ask

What is adb in Java?

Android Debug Bridge (adb) is a versatile command-line tool that lets you communicate with a device. The adb command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device.

Does Appium use adb?

A wrapper over android-adb, implemented using ES6 and along with async/await . This package is mainly used by Appium to perform all adb operations on android device.


1 Answers

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");
like image 57
Marc Van Daele Avatar answered Sep 23 '22 15:09

Marc Van Daele