Runtime.getRuntime.exec hangs

I am using Runtime.getRuntime.exec to create a pdf file from a LaTeX file that I am using to automate the creation of a set of plots:

Runtime.getRuntime.exec(s"pdflatex $name").waitFor

It works fine until the number of plots (at one plot per page) exceeds a few hundred, then it hangs. But if I stop the program and execute the pdflatex command manually at the (Linux/bash) command line, it runs quickly with no problem.

Why would this command run differently using exec than it runs at the command line, and how can I make it run the same? Thanks.

The most likely cause is the classic:

By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream() , getInputStream() , and getErrorStream() . The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

Probably it works fine until the output size exceeds the internal stdout (or stderr) buffer, then it blocks and waits for the buffer(s) to be consumed. You’ll have to (concurrently) read them in order to purge the buffer(s), even if you don’t process the content.

Note that the Scala standard lib provides more convenient ways of handling external process I/O, see e.g. ProcessIO.

Thanks for that information, but does anyone know where I can find a relatively simple solution to this annoying problem? Thanks.


It might not be necessary to use ProcessIO directly – scala.sys.process.ProcessBuilder can return the process output to the caller, or use a scala.sys.process.ProcessLogger which avoids direct interaction with a stream.

What’s missing?