You are waiting for the process to finish before attempting to read its output. But the chances are that it can finish writing all of that output until you've started reading it ... because the OS is not able to buffer it all. Net result, deadlock.
Change the code so that the pro.waitFor()
call happens AFTER you have read all of the output.
(It is also possible that stuff may be written to the 'error' stream. But unless there is something seriously weird happening with the dir
command, it is unlikely that it will generate enough error output to cause a deadlock. So it is probably OK to just ignore any (hypothetical) error output ... as you are currently doing.)