Skip to content

Instantly share code, notes, and snippets.

@th-schwarz
Last active October 27, 2021 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save th-schwarz/041e13ede396a869c7681b5ad637460c to your computer and use it in GitHub Desktop.
Save th-schwarz/041e13ede396a869c7681b5ad637460c to your computer and use it in GitHub Desktop.
The easiest call of the ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("ls", "-al");
pb.directory(Paths.get("/tmp").toFile());
pb.redirectErrorStream(true);
Process process = pb.start();
try (var infoStream = process.getInputStream()) {
infoStream.transferTo(System.out);
}
int exitCode = process.waitFor();
if(exitCode == 0)
System.out.println("Command successful processed.");
else
System.out.println("Command stops with exit-code: " + exitCode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment