【问题标题】:Runtime.getRuntime().exec not working with gcloud [duplicate]Runtime.getRuntime().exec 不适用于 gcloud [重复]
【发布时间】:2020-12-02 02:03:43
【问题描述】:

我试图描述最后执行的数据流作业,以检查特定的数据流作业是否正在运行、停止、失败或使用 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

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

【问题讨论】:

标签: java google-cloud-platform gcloud runtime.exec


【解决方案1】:

ProcessProcessBuilder 类启动进程。

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

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

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

我什至不确定 $( 的东西是 bash 还是 zsh 还是 fish 还是诸如此类,请确保您启动了正确的 shell。

【讨论】:

  • [1/2]感谢您的解决方案。我试过了,但它给了我 null 作为输出。所以我尝试使用命令,它部分工作processBuilderList.command("cmd.exe", "/c","gcloud", "dataflow", "jobs", "list","--sort-by=CREATION_TIME", "--limit=1", "--format=get(id)"); 实际上我想将上面的输出提供给下面提到的另一个命令,如果我在 bash 中运行它,它工作得很好。当我尝试在上述命令中添加“gcloud dataflow jobs describe”时,它给了我 null。
  • [2/2]这是我在 bash 中运行并给我正确输出的内容:gcloud dataflow jobs describe $(gcloud dataflow jobs list --sort-by=CREATION_TIME --limit=1 - -format="get(id)") --format=json 你能建议我如何整合两者吗? job_id=$(gcloud dataflow jobs list --sort-by=CREATION_TIME --limit=1 --format="get(id)" --region=europe-west1) gcloud dataflow jobs describe $job_id --format=json
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 2014-01-05
  • 1970-01-01
  • 2011-01-09
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多