Execute adb commands from java

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 17:34:45

问题


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:


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");


来源:https://stackoverflow.com/questions/11137131/execute-adb-commands-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!