简体   繁体   中英

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

不应该像这样单独的论点

ProcessBuilder proc = new ProcessBuilder("D://android-sdk//platform-tools//adb.exe", "push",  "D:\\final.xml", "/mnt/sdcard/final.xml");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM