[Java] Dealing with the situation where the program that writes to the Output Stream of Process does not end even if waitFor

-[Java] Enter into stdin of Process -Qiita

This is a problem that occurred while doing the contents of the above article.

problem

When I wrote to ʻOutputStream (= stdin)ofProcess, 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);

Action / Cause

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

[Java] Dealing with the situation where the program that writes to the Output Stream of Process does not end even if waitFor
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
A story that suffered from a space that does not disappear even if trimmed with Java The cause is BOM
When using JPA (Hibernate), the Java application does not end even if it reaches the last line of the main method.
[Java] Change the process according to the situation with the Strategy pattern
[Firebase] The problem that the currentUser of Firebase Auth does not become null even if the app is uninstalled.
How to interact with a server that does not crash the app
Output of the book "Introduction to Java"
I tried to make a program that searches for the target class from the process that is overloaded with Java
Setting method that the size does not change even if CSS is changed
The problem that the JDK set in JAVA_HOME does not appear even if java -version is performed at the command prompt
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
MockMVC returns 200 even if I make a request to a path that does not exist
What to do if the JSONHint annotation does not work with lombok and JSONIC
[Java] The problem that uploaded images are not updated due to the influence of cache
Java: Use Stream to sort the contents of the collection
A program (Java) that outputs the sum of odd and even numbers in an array
What to do if you installed Ruby with rbenv but the version does not change
Memo that transitions to the login screen if you are not logged in with devise
How to reduce the load on the program even a little when combining characters with JAVA