Java进程成功执行了 “pdflatex”,但生成的 .pdf 文件为空。

huangapple go评论76阅读模式
英文:

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();
        }
    }
}

Here is the link to all the files

答案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.

huangapple
  • 本文由 发表于 2020年9月2日 22:13:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63707449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定