aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/util/processes/ProcessRunner.java
blob: e7d767156895d54645ad1b31a6a521a1535c793a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package org.shedlang.compiler.util.processes;

import java.io.IOException;
import java.util.List;

public class ProcessRunner {
    private ProcessRunner() {
    }

    /**
     * Run a process until it exits, inheriting standard IO from its parent
     * process.
     */
    public static void runInheritIo(List<String> args) throws InterruptedException, ProcessError {
        var process = startProcessInheritIo(args);

        var exitCode = process.waitFor();
        if (exitCode != 0) {
            throw new ProcessExitError(exitCode);
        }
    }

    private static Process startProcessInheritIo(List<String> args) throws ProcessError {
        try {
            return new ProcessBuilder(args)
                .inheritIO()
                .start();
        } catch (IOException error) {
            throw new ProcessStartError(error);
        }
    }

}