英文:
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
Process
和 ProcessBuilder
类用于启动进程。
它不运行 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论