How to execute Shell Commands with Java and print the output directly while executing the command
public static ProcessBuilder buildProcess(String command, File currentDirectory) {
String runCommand = (System.getProperty("os.name")
.toLowerCase()
.contains("win")? "cmd /" : "sh -")
+ "c " + command;
// split command
StringTokenizer st = new StringTokenizer(runCommand);
String[] cmdarray = new String[st.countTokens()];
for (int i = 0; st.hasMoreTokens(); i++)
cmdarray[i] = st.nextToken();
return new ProcessBuilder(cmdarray)
.directory(currentDirectory).inheritIO();
}