Sure, here’s the translation: 通过Java代码在Git Bash中输入一系列命令

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

Entering a series of commands in git bash through java code

问题

试图在 Git Bash 中依次运行一系列命令。我可以通过代码打开终端,但在那之后无法成功输入任何内容。例如,这是我尝试的代码:

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

请告诉我如何能够通过代码将一系列命令输入到 Git Bash 中。

英文:

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.

答案1

得分: 1

问题在于命令 git-bash.exe 打开了终端窗口,但窗口的输入仍然是键盘输入,因此尝试向方法 getOutputStream() 返回的 OutputStream 写入内容不起作用。请参考这个问题。

作为替代,我建议使用 ProcessBuilder 来执行一系列单独的 git 命令。通过这样做,你的 Java 代码可以获取命令的输出。

以下是一个简单的示例,显示了 git 版本。

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

当你运行上面的代码时,git 版本详情将被写入到 System.out

另外,如果 git 命令失败,错误详情将被写入到 System.err

你需要为每个单独的 git 命令重复上述代码。

英文:

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.

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

发表评论

匿名网友

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

确定