英文:
Java Process executes "pdflatex" successfully but generates empty .pdf file
问题
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();
}
}
}
英文:
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();
}
}
}
答案1
得分: 2
你需要等待进程完成。我猜在等待进程完成之前退出 JVM 会导致 pdflatex
收到一个信号,从而导致它突然终止。
因此,在 p.start()
之后添加一行代码:
p.waitFor();
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论