使用Java中的ProcessBuilder读取输出git-bash [英] Read output git-bash with ProcessBuilder in Java

查看:194
本文介绍了使用Java中的ProcessBuilder读取输出git-bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法读取使用ProcessBuilder在Java应用程序中在git bash中运行的git命令的输出.

I can't read the output of my git command that I run in git bash in my Java application with ProcessBuilder.

OS:Windows 8.1 --- IDE:IntelliJ

OS : Windows 8.1 --- IDE : IntelliJ

我的代码尝试列出github存储库中的所有文件,并计算java文件类型的数量.

My code tries to list all the files in a github repository and count the number of java file types.

完成git命令(管道类型):

Complete git command (pipe type):

cd C:/Users/Utente/Documents/Repository-SVN-Git/Bookkeeper && git ls-files | grep .java | wc -l

结果出现在我的git bash中,但未在我的Java应用程序中显示,我不明白为什么会这样.

The result is present in my git bash but it is not shown in my Java application and I cannot understand why this is so.

Result in git-bash :
 2140
Result in IntelliJ :
 --- Command run successfully
 --- Output=

这是我的java类:

public class SelectMetrics {

public static final String path_bash = "C:/Program Files/Git/git-bash.exe";    
public static final String path_repository = "cd C:/Users/Utente/Documents/Bookkeeper";        
public static final String git_command = "git ls-files | grep .java | wc -l";        
public static final String command_pipe = path_repository + " && " + git_command;

public static void main(String[] args) {       
    runCommandPIPE(command_pipe);
}

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

        Process process = processBuilder.start();
        StringBuilder output = new StringBuilder();
        BufferedReader reader = new BufferedReader(
          new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
        int exitVal = process.waitFor();
        if (exitVal == 0) {
            System.out.println(" --- Command run successfully");
            System.out.println(" --- Output=" + output);
        } else {
            System.out.println(" --- Command run unsuccessfully");
        }
    } catch (IOException | InterruptedException e) {
        System.out.println(" --- Interruption in RunCommand: " + e);
        // Restore interrupted state
        Thread.currentThread().interrupt();
    }
}
  
}

----编辑----

---- EDIT ----

我找到了一种方法,可以通过将git-bash输出打印到txt文件中,然后从我的Java应用程序中读取它来获取.在这里您可以找到代码:

I have found a way to take the git-bash output by printing it in a txt file and then reading it from my java application. Here you can find the code:

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

但是我仍然不明白为什么我无法使用ProcessBuilder读取输出

However I still don't understand why I can't read the output with ProcessBuilder

推荐答案

问题应该出在使用

C:/Program Files/Git/git-bash.exe

因为它打开了用户用来工作的窗口,但是在运行时,您应该在Java应用程序中使用

because it opens the window that a user uses for working, but at runtime in a java application you should use

C:/Program Files/Git/bin/bash.exe

通过这种方式 ProcessBuilder 可以读取git操作的结果.

In this way ProcessBuilder can read the result of the git operations.

ProcessBuilder 无法从窗口 git-bash.exe 读取,并且读取结果为空是正确的.如果您在运行时在git-bash.exe中运行命令,则结果只会显示在 git-bash.exe 窗口中,而Java应用程序无法读取它.

ProcessBuilder cannot read from the window git-bash.exe and it is correct that the result from the reading is null. If you run commands in git-bash.exe at runtime the result will be only shown in the window git-bash.exe and the java application cannot read it.

-编辑2021/03/26 ---

--- EDIT 2021/03/26 ---

最后,要使用git-bash运行命令并在Java应用程序中在运行时从中读取输出,您必须使用以下命令更改我的问题代码:

In conclusion, for run a command with git-bash and read from it the output at runtime in your java application, you have to change my question code with:

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

然后

Result in git-bash :
2183
Result in IntelliJ :
 --- Command run successfully
 --- Output=2183

这篇关于使用Java中的ProcessBuilder读取输出git-bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆