A function that executes a command natively and returns its output as a String.
private static executeCommand(String directory, String[] commandArray) {
ProcessBuilder builder = new ProcessBuilder(commandArray);
if (directory == null)
directory = System.getProperty("user.dir");
builder.directory(new File(directory));
builder.redirectErrorStream(true);
Process p = builder.start();
Scanner scanner = new Scanner(p.getInputStream());
// Build the output string, line by line
String commandOutput = "";
while (scanner.hasNext()) {
commandOutput += scanner.nextLine() + "\n";
}
p.waitFor();
scanner.close();
return commandOutput;
}