I have 2 folders, each containing dozens of batch files (*.bat
).
The batch files containing text similar to either
del /f/q F:\\MEDIA\\IMAGE99
now the output hangs at
waitFor()
When you start an external process from Java using Runtime.exec
you must read any output that the process produces otherwise the process may block (source: JavaDocs for java.lang.Process).
Use ProcessBuilder
instead, and call redirectErrorStream
to merge the standard output and error streams, then read all the content from process.getInputStream()
until you reach EOF. Only then is it safe to call waitFor
.
ProcessBuilder will also help with the spaces issue, as you must split up the command line into individual words yourself
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", file.getAbsolutePath());