java processbuilder example

Solutions on MaxInterview for java processbuilder example by the best coders in the world

showing results for - "java processbuilder example"
Rafael
07 Aug 2019
1public static void main(String[] args) {
2        ProcessBuilder processBuilder = new ProcessBuilder();
3
4        // Run this on Windows, cmd, /c = terminate after this run
5        processBuilder.command("cmd.exe", "/c", "ping -n 3 google.com");
6
7        try {
8            Process process = processBuilder.start();
9			//This will block other tasks
10            BufferedReader reader =
11                    new BufferedReader(new InputStreamReader(process.getInputStream()));
12            String line;
13            while ((line = reader.readLine()) != null) {
14                System.out.println(line);
15            }
16            int exitCode = process.waitFor();
17            System.out.println("\nExited with error code : " + exitCode);
18
19        } catch (Exception e) {
20            e.printStackTrace();
21        } 
22}
23