Runtime.getRuntime().exec 在 gcloud 中不起作用。

huangapple go评论155阅读模式
英文:

Runtime.getRuntime().exec not working with gcloud

问题

I am trying to describe the last executed dataflow job to check whether a specific dataflow job is running, stopped, failed, or executing using java.

我正在尝试描述最后执行的数据流作业,以检查特定的数据流作业是运行中、已停止、失败还是正在执行,使用 Java。

I am trying to execute gcloud command using Runtime.getRuntime().exec(command).

我正在尝试使用 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()));

When I am executing this code I am getting an error as:

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

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

在线翻译到此为止。

英文:

I am trying to describe the last executed dataflow job to check whether a specific dataflow job is running, stopped, failed, or executing using java.

I am trying to execute gcloud command using Runtime.getRuntime().exec(command)

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()));

When I am executing this code I am getting an error as:

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

Can someone help me out here to resolve this error?

答案1

得分: 2

ProcessProcessBuilder 类用于启动进程。

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

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

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

我甚至不确定 $( 这些是 bash 还是 zsh 还是 fish 或其他什么,确保你启动了正确的 shell。

英文:

The Process and ProcessBuilder classes launches processes.

It does not run bash shell commands, but that's what you typed. You can't run this.

What you can try to do is start bash, and pass this as the command for bash to execute. Don't use runtime.exec, use ProcessBuilder, don't pass the command as a single string, pass each argument as a separate string. Try:

List.of("/bin/bash", "-c", "gcloud... all that mess"); as command.

I'm not even sure if that $( stuff is bash or zsh or fish or whatnot, make sure you start the right shell.

huangapple
  • 本文由 发表于 2020年8月12日 18:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63374552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定