使用processBuilder打开git bash并在其中执行命令

时间:2018-11-07 13:01:29

标签: java cmd process inputstream processbuilder

在Java中是否可以通过使用ProcessBuilder之类的工具打开gitbash,编写命令(例如git status)并输出结果?

我可以使用以下代码成功打开git bash,但我不知道如何在其中编写任何命令。

String[] commands = {"cmd","/C","C:\\Users\\......\\Git\git-bash"};
ProcessBuilder builder = new ProcessBuilder(commands);
builder.redirectErrorStream(true);
Process process = builder.start();

StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try
{
   br=new BufferedReader(new InputStreamReader(process.getInputStream()));
   String line = null;
   while ((line = br.readLine()) != null) {
      sb.append(line + System.getProperty("line.seperator"));
   }
} finally {
 br.close();
}

String outcome = get_output(process.getInputStream());
process.waitFor();
System.out.println("Process finished with outcome = " + outcome);

1 个答案:

答案 0 :(得分:1)

您只需要更改路径和git命令。但是git-bash输出显示在单独的.txt文件中,因为我无法以其他任何方式读取它。

public class GitBash {

    public static final String path_bash = "C:/Program Files/Git/git-bash.exe";

    // Create a file Output.txt where git-bash prints the results
    public static final String path_file_output_git_bash =
            "C:/Users/Utente/Documents/IntelliJ-DOC/IntelliJ_project/Prova/src/main/Git-bash/Output.txt";

    public static void main(String[] args) {
        // Path to your repository
        String path_repository = "cd C:/Users/Utente/Documents/Repository-SVN-Git/Bookkeeper";
        // Git command you want to run
        String git_command = "git ls-files | grep .java | wc -l";

        String command = path_repository + " && " + git_command + " > " + path_file_output_git_bash;

        runCommand(command);
    }

    public static void runCommand(String command) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder();
            processBuilder.command(path_bash, "-c", command);

            Process process = processBuilder.start();

            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println(" --- Command run successfully");
                System.out.println(" --- Output = " + readFileTxt());

            } else {
                System.out.println(" --- Command run unsuccessfully");
            }
        } catch (IOException | InterruptedException e) {
            System.out.println(" --- Interruption in RunCommand: " + e);
            // Restore interrupted state
            Thread.currentThread().interrupt();
        }
    }

    public static String readFileTxt() {
        String data = null;
        try {
            File myObj = new File(path_file_output_git_bash);
            Scanner myReader = new Scanner(myObj);
            while (myReader.hasNextLine()) {
                data = myReader.nextLine();
            }
            myReader.close();
        } catch (FileNotFoundException e) {
            System.out.println(" --- An error occurred");
            e.printStackTrace();
            }
            return data;
        }
    }
}