Skip to content
Advertisement

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.

import java.io.File;
import java.io.IOException;

class HelloWorld {
    public static void main(String[] args) {
        try {
            ProcessBuilder pb = new ProcessBuilder("pdflatex", "tarea0.tex");
            pb.directory(new File("/Users/carlosreategui/coding/java_testing/latex"));
            Process p = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here is the link to all the files

Advertisement

Answer

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:

p.waitFor();

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

Advertisement