Java : ProcessBuilder.Redirect with Examples

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


Summary

Represents a source of subprocess input or a destination of subprocess output.

Class diagram

Please see also : ProcessBuilder

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

        final var builder = new ProcessBuilder("java", "Child");
        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
//  OUT 3
//waitFor : 0
//main : end

Fields

static final ProcessBuilder.Redirect DISCARD

Indicates that subprocess output will be discarded.

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

        final var builder = new ProcessBuilder("java", "Child");
        if (args.length == 1 && "discard".equals(args[0])) {
            System.out.println("Discard!");
            builder.redirectOutput(ProcessBuilder.Redirect.DISCARD);
        }
        System.out.println("redirect : " + builder.redirectOutput());

        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 : PIPE
//  OUT 1
//  OUT 2
//  OUT 3
//waitFor : 0
//main : end
//
//> java Main discard
//main : start
//Discard!
//redirect : WRITE
//waitFor : 0
//main : end

static final ProcessBuilder.Redirect INHERIT

Indicates that subprocess I/O source or destination will be the same as those of the current process.

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

        final var builder = new ProcessBuilder("java", "Child");
        if (args.length == 1 && "inherit".equals(args[0])) {
            System.out.println("Inherit!");
            builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        }
        System.out.println("redirect : " + builder.redirectOutput());

        final var process = builder.start();
        System.out.println("waitFor : " + process.waitFor());

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

// Result
// ↓
//> java Main
//main : start
//redirect : PIPE
//waitFor : 0
//main : end
//
//> java Main inherit
//main : start
//Inherit!
//redirect : INHERIT
//  OUT 1
//  OUT 2
//  OUT 3
//waitFor : 0
//main : end

static final ProcessBuilder.Redirect PIPE

Indicates that subprocess I/O will be connected to the current Java process over a pipe.

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

        final var builder = new ProcessBuilder("java", "Child");
        final var redirect = builder.redirectOutput();
        System.out.println("pipe : " + redirect.equals(ProcessBuilder.Redirect.PIPE));

        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
//pipe : true
//  OUT 1
//  OUT 2
//  OUT 3
//waitFor : 0
//main : end

Methods

static ProcessBuilder.Redirect appendTo (File file)

Returns a redirect to append to the specified file.

public class Child {
    public static void main(String[] args) {
        System.out.println("OUT 1");
        System.out.println("OUT 2");
        System.out.println("OUT 3");
    }
}
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
                """);

        final var redirect = ProcessBuilder.Redirect.appendTo(file.toFile());
        final var builder = new ProcessBuilder("java", "Child");

        final var process = builder.redirectOutput(redirect).start();
        System.out.println("waitFor : " + process.waitFor());

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

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

boolean equals (Object obj)

Compares the specified object with this Redirect for equality.

final var redirect1 = ProcessBuilder.Redirect.DISCARD;
final var redirect2 = ProcessBuilder.Redirect.INHERIT;

System.out.println(redirect1.equals(redirect2)); // false

final var redirect3 = ProcessBuilder.Redirect.to(Path.of("aaa.txt").toFile());
final var redirect4 = ProcessBuilder.Redirect.to(Path.of("aaa.txt").toFile());
final var redirect5 = ProcessBuilder.Redirect.to(Path.of("bbb.txt").toFile());

System.out.println(redirect3.equals(redirect4)); // true
System.out.println(redirect3.equals(redirect5)); // false

File file ()

Returns the File source or destination associated with this redirect, or null if there is no such file.

System.out.println(ProcessBuilder.Redirect.DISCARD.file()); // NUL
System.out.println(ProcessBuilder.Redirect.INHERIT.file()); // null
System.out.println(ProcessBuilder.Redirect.PIPE.file()); // null

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(ProcessBuilder.Redirect.to(path.toFile()).file()); // R:\java-work\aaa.txt

static ProcessBuilder.Redirect from (File file)

Returns a redirect to read from the specified file.

public class Child {
    public static void main(String[] args) {
        System.out.println("child : 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("child : 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 redirect = ProcessBuilder.Redirect.from(file.toFile());
        final var builder = new ProcessBuilder("java", "Child");

        final var process = builder.redirectInput(redirect).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
//child : start
//  next : abcd
//  next : 1234
//  Quit!
//child : end
//waitFor : 0
//main : end

int hashCode ()

Returns a hash code value for this Redirect.

final var ret1 = ProcessBuilder.Redirect.DISCARD.hashCode();
System.out.println(ret1); // 1277460

final var ret2 = ProcessBuilder.Redirect.INHERIT.hashCode();
System.out.println(ret2); // 573958827

final var ret3 = ProcessBuilder.Redirect.to(Path.of("aaa.txt").toFile()).hashCode();
System.out.println(ret3); // -1239232814

static ProcessBuilder.Redirect to (File file)

Returns a redirect to write to the specified file.

public class Child {
    public static void main(String[] args) {
        System.out.println("OUT 1");
        System.out.println("OUT 2");
        System.out.println("OUT 3");
    }
}
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
                """);

        final var redirect = ProcessBuilder.Redirect.to(file.toFile());
        final var builder = new ProcessBuilder("java", "Child");

        final var process = builder.redirectOutput(redirect).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
//OUT 3

abstract ProcessBuilder.Redirect.Type type ()

Returns the type of this Redirect.

System.out.println(ProcessBuilder.Redirect.DISCARD.type()); // WRITE
System.out.println(ProcessBuilder.Redirect.INHERIT.type()); // INHERIT
System.out.println(ProcessBuilder.Redirect.PIPE.type()); // PIPE

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(ProcessBuilder.Redirect.to(path.toFile()).type()); // WRITE
System.out.println(ProcessBuilder.Redirect.from(path.toFile()).type()); // READ

Related posts

To top of page