Java : ProcessBuilder (外部プロセス起動) - API使用例
ProcessBuilder (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
ProcessBuilder クラスを使うと、Javaプログラムから外部プロセスを起動できます。
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
↓
コンストラクタ
ProcessBuilder (String... command)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//exec : start
// args : [aaa, bbb]
//exec : end
//waitFor : 0
//main : end
ProcessBuilder (List<String> command)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//exec : start
// args : [aaa, bbb]
//exec : end
//waitFor : 0
//main : end
メソッド
List<String> command ()
関連: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)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//exec : start
// args : [aaa, bbb]
//exec : end
//waitFor : 0
//main : end
ProcessBuilder command (List<String> command)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//exec : start
// args : [aaa, bbb]
//exec : end
//waitFor : 0
//main : end
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)
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 ()
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//exec : start
// env1=aaa
// env2=bbb
// env3=ccc
//exec : end
//waitFor : 0
//main : end
関連: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 ()
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");
}
}
// 結果
// ↓
//> java Main
//main : start
// OUT 1
// ERR 1
// OUT 2
// ERR 2
//
//<入力> abcd
// next : abcd
//<入力> 1234
// next : 1234
//<入力> quit
// Quit!
//
//waitFor : 0
//main : end
ProcessBuilder.Redirect redirectError ()
関連: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)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//waitFor : 0
//main : end
//
//> cat aaa.txt
//ERR 1
//ERR 2
ProcessBuilder redirectError (ProcessBuilder.Redirect 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");
}
}
// 結果
// ↓
//> java Main
//main : start
// ERR 1
// ERR 2
//waitFor : 0
//main : end
boolean redirectErrorStream ()
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");
}
}
// 結果
// ↓
//> 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)
このメソッドの使用例は、redirectErrorStream() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
ProcessBuilder.Redirect redirectInput ()
関連: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)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//exec : start
// next : abcd
// next : 1234
// Quit!
//exec : end
//waitFor : 0
//main : end
ProcessBuilder redirectInput (ProcessBuilder.Redirect 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");
}
}
// 結果
// ↓
//> java Main
//main : start
//exec : start
//
//<入力> abcd
// next : abcd
//<入力> 1234
// next : 1234
//<入力> quit
// Quit!
//
//exec : end
//waitFor : 0
//main : end
ProcessBuilder.Redirect redirectOutput ()
関連: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)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//waitFor : 0
//main : end
//
//> cat aaa.txt
//OUT 1
//OUT 2
ProcessBuilder redirectOutput (ProcessBuilder.Redirect 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");
}
}
// 結果
// ↓
//> java Main
//main : start
// OUT 1
// OUT 2
//waitFor : 0
//main : end
Process start ()
このメソッドの使用例は、ProcessBuilder(String... command) にまとめて記載しました。
そちらのAPI使用例をご参照ください。
static List<Process> startPipeline (List<ProcessBuilder> builders)
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");
}
}
// 結果
// ↓
//> java Main
//main : start
//waitFor : 0
//waitFor : 0
//main : end
//
//> cat bbb.txt
//exec B : start
// next : abcd
// next : 1234
// Quit!
//exec B : end