我正在尝试运行以下代码:
public class StandOnLinux {
public static void main(String[] args) throws IOException {
//String commandToBeExecuted = "git blame '"+files[i]+"' | grep "+revision+" | awk '{print $7}'";
//String commandToBeExecuted = "git --git-dir D:\\code_coverage\\.git blame src\\RunCodeCoverage.java | grep 'e4ecfbe'"; git blame --git-dir D:/code_coverage/.git src/RunCodeCoverage.java| grep 59e3fdc | gawk '{print $7}'
String commandToBeExecuted = "git blame StandOnLinux.java --grep=\"e8a93e7d\"";
String temp = "";
File file = new File("C:/Program Files (x86)/Git/bin");
System.out.println(commandToBeExecuted);
Process p = Runtime.getRuntime().exec(commandToBeExecuted);
//Process p = Runtime.getRuntime().exec(new String[]{"cmd", "/c", commandToBeExecuted});
//Process p =Runtime.getRuntime().exec(commandToBeExecuted, null, file);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
while((temp = stdInput.readLine()) != null) {
System.out.println(temp);
}
while((temp = stdError.readLine()) != null) {
System.out.println(temp);
}
}
}
但是这给了我结果而没有grep输出,如grep。
中所述有人能说出我在这段代码中做了什么错误吗?
在Linux机器上运行此代码。
答案 0 :(得分:0)
要使管道正常工作,您可以尝试在单独的shell中执行命令,例如" How to make pipes work with Runtime.exec()
?"
String[] cmd = {
"/bin/sh",
"-c",
"git blame StandOnLinux.java | grep e8a93e7d"
};
Process p = Runtime.getRuntime().exec(cmd);
确保默认$PATH
包含git
。