LoginSignup
1
0

More than 3 years have passed since last update.

【Java】ProcessのOutputStreamに書き込みを行うプログラムでwaitForしても終了しなくなる状況への対処

Last updated at Posted at 2019-08-17

上記記事の内容をやっている時に発生した問題です。

問題

ProcessOutputStream(= stdin)に書き込みを行ったところ、時間制限付きでwaitForしても終了しなくなりました。

問題の出たコード
// Exceptionのtry-catchは外でやっている想定
Process process = new ProcessBuilder(commands).start();

// stdinへの書き込み
OutputStream os = process.getOutputStream();
os.write(multipartFile.getBytes());

boolean result = process.waitFor(5, TimeUnit.SECONDS);

対処 / 原因

OutputStreamを閉じるようにしたところ解決したため、恐らく延々書き込み待ちするようになったことが原因だと思います。

対処後
// Exceptionのtry-catchは外でやっている想定
Process process = new ProcessBuilder(commands).start();

// stdinへの書き込み
try (OutputStream os = process.getOutputStream()) {
  os.write(multipartFile.getBytes());
}

boolean result = process.waitFor(5, TimeUnit.SECONDS);
1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0