运行 windows cmd 中的多个命令使用 java 中的进程 class

run multiple commands in windows cmd using Process class in java

我正在使用 java 应用程序创建 .pdf 文件。它写入 .tex 文件,以便 Miktex 可以创建 pdf。

writePDF(s);
    String command = "cmd /c start xelatex -synctex=1 -interaction=nonstopmode " + s + ".tex && del " + s + ".tex";  
    Runtime r = Runtime.getRuntime();
    p = r.exec(command);
    p.waitFor();

但唯一发生的事情是 textput.log 使用以下内容创建:

entering extended mode
**aa.tex

! Emergency stop.
<*> aa.tex

*** (job aborted, file error in nonstop mode)

奇怪的是,当我 运行 直接在 windows cmd 上执行该命令时,它工作正常。如果我也像这样制作 "command" 变量并使用 java 应用程序 运行 它也可以正常工作。

String command = "cmd /c start xelatex -synctex=1 -interaction=nonstopmode " + s + ".tex" 

我正在使用 java 8 和 Miktex 2.9.7 希望你能帮忙

因此,为了按顺序(不是并行)执行 Java 中的多个命令,您可以尝试 运行 像这样:

    String[] commands = new String[] {"xelatex -synctex=1 -interaction=nonstopmode " + s + ".tex"
                                   , "del " + s + ".tex"};  
    Runtime r = Runtime.getRuntime();
    Process p;
    for (String command: commands) {
      p = r.exec(command);
      p.waitFor();
    }

除了@Brother 已经说过的如何调用多个命令,你必须看这里:https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html

本文解释了如何正确使用 Runtime.exec(),即您必须完全使用所有进程输出流(标准和错误)。