Varejator
Varejator

Reputation: 17

run multiple commands in windows cmd using Process class in java

Im using a java application for creating a .pdf file. It writes the .tex file so Miktex can create a 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:

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. If i also make the "command" variable like this and run it withing java app it works fine aswell.

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

Upvotes: 0

Views: 651

Answers (2)

Brother
Brother

Reputation: 2210

So, in order to execute multiple commands in Java in sequence (not parallel), you can try to run like this:

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

Upvotes: 0

davidbuzatto
davidbuzatto

Reputation: 9424

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

This article explains how to use Runtime.exec() properly, i.e., you must consume all process output streams (standard and error) completely.

Upvotes: 0

Related Questions