Skip to content

Instantly share code, notes, and snippets.

@packmad
Created February 20, 2018 09:42
Show Gist options
  • Save packmad/a7fe38015c539204b9d958c98209014a to your computer and use it in GitHub Desktop.
Save packmad/a7fe38015c539204b9d958c98209014a to your computer and use it in GitHub Desktop.
Correct way to create another process and execute an external command
static List<String> execute(final List<String> args) throws IOException, InterruptedException {
final List<String> output = new LinkedList<>();
ProcessBuilder pb = new ProcessBuilder(args);
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
output.add(line);
}
process.waitFor();
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment