-[Java] Enter into stdin of Process -Qiita
This is a problem that occurred while doing the contents of the above article.
When I wrote to ʻOutputStream (= stdin)of
Process, it did not end even if
waitFor` with a time limit.
Problematic code
//Exception try-Assuming that catch is done outside
Process process = new ProcessBuilder(commands).start();
//Write to stdin
OutputStream os = process.getOutputStream();
os.write(multipartFile.getBytes());
boolean result = process.waitFor(5, TimeUnit.SECONDS);
I tried to close OutputStream` and it was solved, so I think it was probably because I had to wait for writing endlessly.
After dealing
//Exception try-Assuming that catch is done outside
Process process = new ProcessBuilder(commands).start();
//Write to stdin
try (OutputStream os = process.getOutputStream()) {
os.write(multipartFile.getBytes());
}
boolean result = process.waitFor(5, TimeUnit.SECONDS);
Recommended Posts