Runtime.getRuntime().exec 不适用于 gcloud

Runtime.getRuntime().exec not working with gcloud

我正在尝试描述上次执行的数据流作业以检查特定数据流作业是否 运行、已停止、失败或正在使用 java 执行。

我正在尝试使用 Runtime.getRuntime().exec(command)

执行 gcloud 命令
String command ="gcloud dataflow jobs describe $(gcloud dataflow jobs list --sort-by=CREATION_TIME --limit=1 --format=\"get(id)\") --format=json";
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec(command);
BufferedReader is = new BufferedReader(new
InputStreamReader(process.getInputStream()));

当我执行这段代码时,出现错误:

Exception in thread "main" java.io.IOException: Cannot run program "gcloud": CreateProcess error=2, The system cannot find the file specified

有人可以帮我解决这个错误吗?

ProcessProcessBuilder 类 启动进程。

它没有 运行 bash shell 命令,但这是您键入的内容。你不能运行这个。

您可以尝试做的是启动 bash,并将其作为命令传递给 bash 以执行。不要使用 runtime.exec,使用 ProcessBuilder,不要将命令作为单个字符串传递,将每个参数作为单独的字符串传递。尝试:

List.of("/bin/bash", "-c", "gcloud... all that mess"); 作为命令。

我什至不确定那个 $( 东西是 bash 还是 zsh 或 fish 或诸如此类的东西,确保你开始正确 shell。