1

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?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Panda
  • 513
  • 2
  • 11
  • Does this answer your question? [Run cmd commands through Java](https://stackoverflow.com/questions/15464111/run-cmd-commands-through-java) – Jan Hernandez Aug 12 '20 at 14:00

1 Answers1

2

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • 1
    [1/2]Thanks for the solution. I tried it but It was giving me null as output. so i tried with command and it partially worked `processBuilderList.command("cmd.exe", "/c","gcloud", "dataflow", "jobs", "list","--sort-by=CREATION_TIME", "--limit=1", "--format=get(id)");` Actually I want to give the output from above to another command that mentioned below and it is working perfectly if I run it in bash. When I try to add 'gcloud dataflow jobs describe' in the above-mentioned command it gives me null. – Panda Aug 12 '20 at 14:04
  • 1
    [2/2]This is what I am running in bash and giving me correct output: gcloud dataflow jobs describe $(gcloud dataflow jobs list --sort-by=CREATION_TIME --limit=1 --format="get(id)") --format=json Can you please suggest how can I integrate both? `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` – Panda Aug 12 '20 at 14:05