问题
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