0

Hello I am trying to issue gcloud command within a java app using process builder.

public static void sendCmd() {
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("cmd.exe", "/c", "gcloud");//"");

    try {
        Process process = processBuilder.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        int exitCode = process.waitFor();
        System.out.println("\nExited with error code : " + exitCode);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

The result:

Exited with error code : 1 Process finished with exit code 0

If I type 'gcloud' into command prompt I receive the full help message - as expected.

gcloud not recognized as an internal or external command on Windows gcloud not recognized as an internal or external command on Windows

I've looked through multiple threads - my PATH is set correct 'C:\Users\Home\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin'

and I've added ';.PY' to PATHEXT.

gcloud works fine in cmd prompt but cant access it programmatically?

  • Could you try adding this line `builder.redirectErrorStream(true);` to the code as shown in this [thread](https://stackoverflow.com/questions/15464111/run-cmd-commands-through-java) to try to log why the command crashed? Right now it's hard to say what may be happening. – Happy-Monad Jul 16 '20 at 11:51

0 Answers0