1

Am trying to get a series of commands on git bash one after the other. I can open the terminal through the code but after that wasn't successful with entering anything. For instance this is the code I tried

 String [] args = new String[] {"C:\\Program Files\\Git\\git-bash.exe"};
                String something="hi how are you doing";

                try {
                    ProcessBuilder p = new ProcessBuilder();
                    var proc = p.command(args).start();
                    var w = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
                    w.write(something);
                } catch (IOException ioException){
                    System.out.println(ioException);
                }

Please let know how to be able to do enter a series of commands into git bash through the code.

3
  • What do you need to actually do? Commented Aug 22, 2020 at 16:37
  • I want to enter a series of commands after the terminal opens. I was thinking of making a basic UI interface with some buttons wherein when I click on a button then some actions follow in the terminal. Commands like this I enter into the terminal - export REGISTRY_IP=$(kubectl get service eclipse-hono-service-device-registry-ext --output="jsonpath={.status.loadBalancer.ingress[0]['hostname','ip']}" -n hono) export HTTP_ADAPTER_IP=$(kubectl get service eclipse-hono-adapter-http-vertx --output="jsonpath={.status.loadBalancer.ingress[0]['hostname','ip']}" -n hono) Commented Aug 22, 2020 at 16:49
  • 1
    The normal place to do such exports is in the bash configuration file. Sure that will not do for you? Commented Aug 22, 2020 at 17:05

1 Answer 1

1

The problem is that the command git-bash.exe opens the terminal window but the window's input is still the keyboard, so trying to write to the OutputStream that is returned by method getOutputStream(), in class Process does nothing. Refer to this question.

As an alternative, I suggest using using ProcessBuilder to execute a series of individual git commands. When you do that, your java code gets the command output.

Here is a simple example that displays the git version.

import java.io.IOException;

public class ProcBldT4 {

    public static void main(String[] args) {
        // C:\Program Files\Git\git-bash.exe
        // C:\Program Files\Git\cmd\git.exe
        ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Git\\cmd\\git.exe", "--version");
        pb.inheritIO();
        try {
            Process proc = pb.start();
            int exitStatus = proc.waitFor();
            System.out.println(exitStatus);
        }
        catch (IOException | InterruptedException x) {
            x.printStackTrace();
        }
    }
}

When you run the above code, the git version details will be written to System.out.

Also, if the git command fails, the error details are written to System.err.

You need to repeat the code above for each, individual git command that you need to issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.