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
My code tries to list all the files in a github repository and count the number of java file types.
Complete git command (pipe type):
cd C:/Users/Utente/Documents/Repository-SVN-Git/Bookkeeper && git ls-files | grep .java | wc -l
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=
This is my class 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 ----
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:
Open git bash using processBuilder and execute command in it
However I still don't understand why I can't read the output with ProcessBuilder
The problem should be in the use of
C:/Program Files/Git/git-bash.exe
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
In this way ProcessBuilder can read the result of the git operations.
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.
--- EDIT 2021/03/26 ---
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";
then
Result in git-bash :
2183
Result in IntelliJ :
--- Command run successfully
--- Output=2183