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