getRuntime with pipe?

浪尽此生 提交于 2019-12-25 07:27:15

问题


I want to execute the following command in a java program. But it execute only the first part. the part after | is not executed

Process process = Runtime.getRuntime().exec(" adb devices | tail -n +2 | cut -sf 1");

        process.waitFor();

回答1:


You need to run that in a shell. Try this:

Process process = Runtime.getRuntime().exec(new String[] {
        "/bin/sh",
        "-c",
        "adb devices | tail -n +2 | cut -sf 1"
});
process.waitFor();



回答2:


Use a script instead because Pipe itself is a part of the shell.

OR do something like this:

String[] commands = {
"/bin/sh",
"-c",
"ls /etc | grep release"
};

Process p = Runtime.getRuntime().exec(commands);


来源:https://stackoverflow.com/questions/15834656/getruntime-with-pipe

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