Java : ProcessBuilder.Redirect - API使用例
ProcessBuilder.Redirect (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ProcessBuilder.Redirect クラスは、ProcessBuilder で起動する外部プロセスの
- 標準出力
- 標準入力
- 標準エラー出力
をどのように接続するかを表します。
例えば INHERIT を使うと、親のプロセスと同じ標準出力を使うことができます。
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");
}
}
// 結果
// ↓
//> java Main
//main : start
// OUT 1
// OUT 2
// OUT 3
//waitFor : 0
//main : end
フィールド
static final ProcessBuilder.Redirect DISCARD
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");
}
}
// 結果
// ↓
//> 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
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");
}
}
// 結果
// ↓
//> 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
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//pipe : true
// OUT 1
// OUT 2
// OUT 3
//waitFor : 0
//main : end
メソッド
static ProcessBuilder.Redirect appendTo (File 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");
}
}
// 結果
// ↓
//> java Main
//main : start
//waitFor : 0
//main : end
//
//> cat aaa.txt
//abcd
//1234
//OUT 1
//OUT 2
//OUT 3
boolean equals (Object obj)
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 ()
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)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//child : start
// next : abcd
// next : 1234
// Quit!
//child : end
//waitFor : 0
//main : end
int hashCode ()
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)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//waitFor : 0
//main : end
//
//> cat aaa.txt
//OUT 1
//OUT 2
//OUT 3
abstract ProcessBuilder.Redirect.Type type ()
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