简体   繁体   English

在Windows cmd中使用Java中的Process类运行多个命令

[英]run multiple commands in windows cmd using Process class in java

Im using a java application for creating a .pdf file. 我正在使用Java应用程序创建.pdf文件。 It writes the .tex file so Miktex can create a 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();

but the only thing that happens is textput.log being created with the following contents: 但是唯一发生的是使用以下内容创建的textput.log:

entering extended mode
**aa.tex

! Emergency stop.
<*> aa.tex

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

The strange thing is that when i run that command directly on windows cmd it works fine. 奇怪的是,当我直接在Windows cmd上运行该命令时,它工作正常。 If i also make the "command" variable like this and run it withing java app it works fine aswell. 如果我也像这样制作“命令”变量,并通过java应用程序运行它,那么它也可以正常工作。

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

I'm using java 8 and Miktex 2.9.7 Hope you can help 我正在使用Java 8和Miktex 2.9.7,希望能对您有所帮助

So, in order to execute multiple commands in Java in sequence (not parallel), you can try to run like this: 因此,为了依次(而不是并行)执行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();
    }

Besides how to call multiple commands, as @Brother already said, you MUST take a look here: https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html 正如@Brother已经说过的那样,除了如何调用多个命令外,您还必须在这里看看: https ://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html

This article explains how to use Runtime.exec() properly, ie, you must consume all process output streams (standard and error) completely. 本文介绍了如何正确使用Runtime.exec() ,即,您必须完全消耗所有进程输出流(标准和错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM