Search code examples
javamiktexcm

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


Solution

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