Last active
October 27, 2021 11:17
-
-
Save th-schwarz/041e13ede396a869c7681b5ad637460c to your computer and use it in GitHub Desktop.
The easiest call of the ProcessBuilder
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
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