0

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

12
  • Hi @Varejator, I added an example of how to achieve this.
    – Brother
    Jul 15, 2019 at 16:09
  • doesn't work. it gives a message back: cannot run program ... create processerror=2 system cannot find specific file. but it wrote the .tex file
    – Varejator
    Jul 15, 2019 at 16:14
  • cannot run program cmd /c start (and so on) create ProcessorError=2 System cannot find specified file.
    – Varejator
    Jul 15, 2019 at 16:18
  • And from my example, if you remove the second command "del " + s + ".tex" and leave the array with 1 element, does it work without the message?
    – Brother
    Jul 15, 2019 at 16:19
  • doesnt work, only if use String not String[]
    – Varejator
    Jul 15, 2019 at 16:22

2 Answers 2

0

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();
    }
2
  • hey @varejator, according to other places, you can run "xelatex" without specifying "cmd /c start " that causes to run in another terminal. that it is probably why it runs in parallel
    – Brother
    Jul 17, 2019 at 10:02
  • it works that way, the only problem now is that first process doesnt finish but at least the 2nd one doesnt start
    – Varejator
    Jul 17, 2019 at 10:49
0

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.

2
  • Gonna see that but shouldn't it give the same problem if i use only one command?
    – Varejator
    Jul 15, 2019 at 16:24
  • @Varejator for one or n commands you must consume all output streams. Read the article! Jul 16, 2019 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.