Java : ProcessBuilder with Examples

ProcessBuilder (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ProcessBuilder methods.


Summary

This class is used to create operating system processes.

Class diagram

An example of starting Notepad on Windows.

final var builder = new ProcessBuilder("notepad");
final var process = builder.start();
System.out.println(process); // Process[pid=14796, exitValue="not exited"]

final var ret = process.waitFor();
System.out.println(ret); // 0

 ↓

Notepad


Constructors

ProcessBuilder (String... command)

Constructs a process builder with the specified operating system program and arguments.

public class Exec {
    public static void main(String[] args) {
        System.out.println("exec : start");
        System.out.println("  args : " + Arrays.toString(args));
        System.out.println("exec : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var builder = new ProcessBuilder("java", "Exec", "aaa", "bbb");
        final var process = builder.start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//exec : start
//  args : [aaa, bbb]
//exec : end
//waitFor : 0
//main : end

ProcessBuilder (List<String> command)

Constructs a process builder with the specified operating system program and arguments.

public class Exec {
    public static void main(String[] args) {
        System.out.println("exec : start");
        System.out.println("  args : " + Arrays.toString(args));
        System.out.println("exec : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var command = List.of("java", "Exec", "aaa", "bbb");
        final var builder = new ProcessBuilder(command);
        final var process = builder.start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//exec : start
//  args : [aaa, bbb]
//exec : end
//waitFor : 0
//main : end

Methods

List<String> command ()

Returns this process builder's operating system program and arguments.

Please see also : command(String... command), command(List<String> command)

final var builder = new ProcessBuilder();
System.out.println(builder.command()); // []

builder.command("java", "Main");
System.out.println(builder.command()); // [java, Main]

ProcessBuilder command (String... command)

Sets this process builder's operating system program and arguments.

public class Exec {
    public static void main(String[] args) {
        System.out.println("exec : start");
        System.out.println("  args : " + Arrays.toString(args));
        System.out.println("exec : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var builder = new ProcessBuilder();
        final var process = builder.command("java", "Exec", "aaa", "bbb").start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//exec : start
//  args : [aaa, bbb]
//exec : end
//waitFor : 0
//main : end

ProcessBuilder command (List<String> command)

Sets this process builder's operating system program and arguments.

public class Exec {
    public static void main(String[] args) {
        System.out.println("exec : start");
        System.out.println("  args : " + Arrays.toString(args));
        System.out.println("exec : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var command = List.of("java", "Exec", "aaa", "bbb");

        final var builder = new ProcessBuilder();
        final var process = builder.command(command).start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//exec : start
//  args : [aaa, bbb]
//exec : end
//waitFor : 0
//main : end

File directory ()

Returns this process builder's working directory.

Please see also : directory(File directory)

final var dir = Path.of("R:", "java-work", "dir");
System.out.println(dir); // R:\java-work\dir

final var builder = new ProcessBuilder();
System.out.println(builder.directory()); // null

builder.directory(dir.toFile());
System.out.println(builder.directory()); // R:\java-work\dir

ProcessBuilder directory (File directory)

Sets this process builder's working directory.

public class Exec {
    public static void main(String[] args) {
        System.out.println("exec : start");
        System.out.println("  user.dir=" + System.getProperty("user.dir"));
        System.out.println("exec : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var dir = Path.of("R:", "java-work", "dir");

        final var builder = new ProcessBuilder("java", "Exec");
        final var process = builder.directory(dir.toFile()).start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//│  Main.class
//│  Main.java
//│
//└─dir
//        Exec.class
//        Exec.java
//
//PS R:\java-work> java Main
//main : start
//exec : start
//  user.dir=R:\java-work\dir
//exec : end
//waitFor : 0
//main : end

Map<String,String> environment ()

Returns a string map view of this process builder's environment.

public class Exec {
    public static void main(String[] args) {
        System.out.println("exec : start");
        System.out.println("  env1=" + System.getenv("env1"));
        System.out.println("  env2=" + System.getenv("env2"));
        System.out.println("  env3=" + System.getenv("env3"));
        System.out.println("exec : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var builder = new ProcessBuilder("java", "Exec");
        final var env = builder.environment();
        env.put("env1", "aaa");
        env.put("env2", "bbb");
        env.put("env3", "ccc");

        final var process = builder.start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//exec : start
//  env1=aaa
//  env2=bbb
//  env3=ccc
//exec : end
//waitFor : 0
//main : end

Please see also : System.getenv

final var builder = new ProcessBuilder();
final var env = builder.environment();
System.out.println(env.size()); // 45
System.out.println(env.get("OS")); // Windows_NT
System.out.println(env.get("PUBLIC")); // C:\Users\Public

ProcessBuilder inheritIO ()

Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

public class Exec {
    public static void main(String[] args) {
        System.out.println("  OUT 1");
        System.err.println("  ERR 1");
        System.out.println("  OUT 2");
        System.err.println("  ERR 2");

        final var sc = new Scanner(System.in);
        while (sc.hasNext()) {
            final var next = sc.next();

            if ("quit".equals(next)) {
                System.out.println("  Quit!");
                break;
            }

            System.out.println("  next : " + next);
        }
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var builder = new ProcessBuilder("java", "Exec");
        final var process = builder.inheritIO().start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//  OUT 1
//  ERR 1
//  OUT 2
//  ERR 2
//
//<Input> abcd
//  next : abcd
//<Input> 1234
//  next : 1234
//<Input> quit
//  Quit!
//
//waitFor : 0
//main : end

ProcessBuilder.Redirect redirectError ()

Returns this process builder's standard error destination.

Please see also : redirectError(ProcessBuilder.Redirect destination), redirectError(File file)

final var builder = new ProcessBuilder();
System.out.println(builder.redirectError()); // PIPE

builder.redirectError(ProcessBuilder.Redirect.INHERIT);
System.out.println(builder.redirectError()); // INHERIT

final var file = Path.of("aaa.txt");
builder.redirectError(file.toFile());
System.out.println(builder.redirectError()); // redirect to write to file "aaa.txt"

ProcessBuilder redirectError (File file)

Sets this process builder's standard error destination to a file.

public class Exec {
    public static void main(String[] args) {
        System.out.println("OUT 1");
        System.err.println("ERR 1");
        System.out.println("OUT 2");
        System.err.println("ERR 2");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var file = Path.of("aaa.txt");

        final var builder = new ProcessBuilder("java", "Exec");
        final var process = builder.redirectError(file.toFile()).start();
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//waitFor : 0
//main : end
//
//> cat aaa.txt
//ERR 1
//ERR 2

ProcessBuilder redirectError (ProcessBuilder.Redirect destination)

Sets this process builder's standard error destination.

public class Exec {
    public static void main(String[] args) {
        System.out.println("  OUT 1");
        System.err.println("  ERR 1");
        System.out.println("  OUT 2");
        System.err.println("  ERR 2");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var builder = new ProcessBuilder("java", "Exec");
        final var process = builder.redirectError(ProcessBuilder.Redirect.INHERIT).start();
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//  ERR 1
//  ERR 2
//waitFor : 0
//main : end

boolean redirectErrorStream ()

Tells whether this process builder merges standard error and standard output.

public class Exec {
    public static void main(String[] args) {
        System.out.println("  OUT 1");
        System.err.println("  ERR 1");
        System.out.println("  OUT 2");
        System.err.println("  ERR 2");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var builder = new ProcessBuilder("java", "Exec");

        if (args.length == 1 && "merge".equals(args[0])) {
            builder.redirectErrorStream(true);
        }
        System.out.println("redirect error stream : " + builder.redirectErrorStream());

        final var process = builder.start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//redirect error stream : false
//  OUT 1
//  OUT 2
//waitFor : 0
//main : end
//
//> java Main merge
//main : start
//redirect error stream : true
//  OUT 1
//  ERR 1
//  OUT 2
//  ERR 2
//waitFor : 0
//main : end

ProcessBuilder redirectErrorStream (boolean redirectErrorStream)

Sets this process builder's redirectErrorStream property.

Please see redirectErrorStream().

ProcessBuilder.Redirect redirectInput ()

Returns this process builder's standard input source.

Please see also : redirectInput(ProcessBuilder.Redirect source), redirectInput(File file)

final var builder = new ProcessBuilder();
System.out.println(builder.redirectInput()); // PIPE

builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
System.out.println(builder.redirectInput()); // INHERIT

final var file = Path.of("aaa.txt");
builder.redirectInput(file.toFile());
System.out.println(builder.redirectInput()); // redirect to read from file "aaa.txt"

ProcessBuilder redirectInput (File file)

Sets this process builder's standard input source to a file.

public class Exec {
    public static void main(String[] args) throws IOException {
        System.out.println("exec : start");

        final var sc = new Scanner(System.in);
        while (sc.hasNext()) {
            final var next = sc.next();

            if ("quit".equals(next)) {
                System.out.println("  Quit!");
                break;
            }

            System.out.println("  next : " + next);
        }

        System.out.println("exec : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var file = Path.of("aaa.txt");
        Files.writeString(file, """
                abcd
                1234
                quit
                """);

        final var builder = new ProcessBuilder("java", "Exec");
        final var process = builder.redirectInput(file.toFile()).start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//exec : start
//  next : abcd
//  next : 1234
//  Quit!
//exec : end
//waitFor : 0
//main : end

ProcessBuilder redirectInput (ProcessBuilder.Redirect source)

Sets this process builder's standard input source.

public class Exec {
    public static void main(String[] args) throws IOException {
        System.out.println("exec : start");

        final var sc = new Scanner(System.in);
        while (sc.hasNext()) {
            final var next = sc.next();

            if ("quit".equals(next)) {
                System.out.println("  Quit!");
                break;
            }

            System.out.println("  next : " + next);
        }

        System.out.println("exec : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var builder = new ProcessBuilder("java", "Exec");
        final var process = builder.redirectInput(ProcessBuilder.Redirect.INHERIT).start();
        try (final var reader = process.inputReader()) {
            reader.lines().forEach(System.out::println);
        }
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//exec : start
//
//<Input> abcd
//  next : abcd
//<Input> 1234
//  next : 1234
//<Input> quit
//  Quit!
//
//exec : end
//waitFor : 0
//main : end

ProcessBuilder.Redirect redirectOutput ()

Returns this process builder's standard output destination.

Please see also : redirectOutput(ProcessBuilder.Redirect destination), redirectOutput(File file)

final var builder = new ProcessBuilder();
System.out.println(builder.redirectOutput()); // PIPE

builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
System.out.println(builder.redirectOutput()); // INHERIT

final var file = Path.of("aaa.txt");
builder.redirectOutput(file.toFile());
System.out.println(builder.redirectOutput()); // redirect to write to file "aaa.txt"

ProcessBuilder redirectOutput (File file)

Sets this process builder's standard output destination to a file.

public class Exec {
    public static void main(String[] args) {
        System.out.println("OUT 1");
        System.err.println("ERR 1");
        System.out.println("OUT 2");
        System.err.println("ERR 2");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var file = Path.of("aaa.txt");

        final var builder = new ProcessBuilder("java", "Exec");
        final var process = builder.redirectOutput(file.toFile()).start();
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//waitFor : 0
//main : end
//
//> cat aaa.txt
//OUT 1
//OUT 2

ProcessBuilder redirectOutput (ProcessBuilder.Redirect destination)

Sets this process builder's standard output destination.

public class Exec {
    public static void main(String[] args) {
        System.out.println("  OUT 1");
        System.err.println("  ERR 1");
        System.out.println("  OUT 2");
        System.err.println("  ERR 2");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var builder = new ProcessBuilder("java", "Exec");
        final var process = builder.redirectOutput(ProcessBuilder.Redirect.INHERIT).start();
        System.out.println("waitFor : " + process.waitFor());

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//  OUT 1
//  OUT 2
//waitFor : 0
//main : end

Process start ()

Starts a new process using the attributes of this process builder.

Please see ProcessBuilder(String... command).

static List<Process> startPipeline (List<ProcessBuilder> builders)

Starts a Process for each ProcessBuilder, creating a pipeline of processes linked by their standard output and standard input streams.

public class ExecA {
    public static void main(String[] args) {
        System.out.println("abcd");
        System.out.println("1234");
        System.out.println("quit");
    }
}
public class ExecB {
    public static void main(String[] args) {
        System.out.println("exec B : start");

        final var sc = new Scanner(System.in);
        while (sc.hasNext()) {
            final var next = sc.next();

            if ("quit".equals(next)) {
                System.out.println("  Quit!");
                break;
            }

            System.out.println("  next : " + next);
        }

        System.out.println("exec B : end");
    }
}
public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("main : start");

        final var file = Path.of("bbb.txt");

        final var builders = List.of(
                new ProcessBuilder("java", "ExecA"),
                new ProcessBuilder("java", "ExecB").redirectOutput(file.toFile())
        );

        final var processes = ProcessBuilder.startPipeline(builders);
        for (final var process : processes) {
            System.out.println("waitFor : " + process.waitFor());
        }

        System.out.println("main : end");
    }
}

// Result
// ↓
//> java Main
//main : start
//waitFor : 0
//waitFor : 0
//main : end
//
//> cat bbb.txt
//exec B : start
//  next : abcd
//  next : 1234
//  Quit!
//exec B : end

Related posts

To top of page