Execute adb commands from java

2019-02-10 17:05发布

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

1条回答
干净又极端
2楼-- · 2019-02-10 17:39

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");
查看更多
登录 后发表回答