Java Process executes “pdflatex” successfully but generates empty .pdf file


I want to compile a .tex file from a Java program. I wrote the following code, and it successfully executes, but when I try to open the .pdf file generated, the OS pops a message saying that the file is completely empty (link to image).

By the way, when I run the command pdflatex tarea0.tex directly from terminal, it generates the non-empty .pdf file I want to get from the Java program.


class HelloWorld {
    public static void main(String[] args) {
        try {
            ProcessBuilder pb = new ProcessBuilder("pdflatex", "tarea0.tex");
            Process p = pb.start();
        } catch (IOException e) {

Here is the link to all the files


You need to wait for the process to conclude. I’m guessing that exiting the JVM before waiting for the process to conclude causes pdflatex to receive a signal causing it to terminate abruptly.

So adding a line:


after the p.start() should have the desired effect.

Source: stackoverflow