package test_cmd_command;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
public class CommandLine {
public static String executeCommand(String cliCommand) {
String s = null;
BufferedReader stdInput = null;
BufferedReader stdError = null;
String error = "";
String output = "";
try {
ProcessBuilder pb1 = new ProcessBuilder(
"bash",
"-c",
cliCommand);
pb1.redirectErrorStream(true);
Process p = pb1.start();
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) {
output += "\n" + s;
}
//System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
//System.out.println(">> "+s.toString());
error += "\n" + s;
}
} catch (IOException e) {
System.out.println("exception happened - here's what I know: \n" + e.getMessage());
} finally {
try {
stdInput.close();
stdError.close();
} catch (IOException e) {
}
}
String returnValue = null;
if (output != null && error != null) {
returnValue = output + "\n" + ": " + error;
} else if (output != null) {
returnValue = output;
}
return returnValue;
}
}
“ csc -version” 在终端中运行,但不是在MAC上的Java程序中运行。 它给出输出“找不到bash命令” 。 有什么办法可以解决这个问题。 该程序可以正确运行其他命令,例如 javac -version 等。 我正在Windows上的 MAC上上运行此程序。
答案 0 :(得分:0)
这对我有用
export PATH=/Library/Frameworks/Mono.framework/Versions/Versions/bin/:${PATH}
我像这样运行命令“ export PATH = / Library / Frameworks / Mono.framework / Versions / Versions / bin /:$ {PATH}; csc -version” ,它可以正常工作并返回版本的csc。