Created
February 20, 2018 09:42
-
-
Save packmad/a7fe38015c539204b9d958c98209014a to your computer and use it in GitHub Desktop.
Correct way to create another process and execute an external command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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