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

Varejator
  • 17
  • 7
  • Hi @Varejator, I added an example of how to achieve this. – Brother Jul 15 '19 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 '19 at 16:14
  • cannot run program cmd /c start (and so on) create ProcessorError=2 System cannot find specified file. – Varejator Jul 15 '19 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 '19 at 16:19
  • doesnt work, only if use String not String[] – Varejator Jul 15 '19 at 16:22
  • Sorry, I dont have a Windows machine .. and if you leave only one command and then remove the "cmd /c " from the start ,does it work? For me it works in Mac. – Brother Jul 15 '19 at 16:22
  • Don't worry, no it doesn't.. guess i will check the website @davidbuzatto had mencioned – Varejator Jul 15 '19 at 16:29
  • guess i've found out something, the 2 processes run parallel. one deletes the file so the other cant find it – Varejator Jul 15 '19 at 16:55
  • @Brother i've found some kind of solution, i write a .bat file with all the commands i want and execute it within a java process. – Varejator Jul 16 '19 at 15:20
  • That is nice @Varejator! .. Another thing you could do is have an array of commands and loop, executing in order. – Brother Jul 17 '19 at 08:44
  • @Brother it keeps running parallel. It works well if i only use native shell commands like "dir","ping" but when i try using an app like xelatex it runs in parallel. What i think its happening is that when it calls xelatex it creates another process so the current one keeps looping though commands anyway. – Varejator Jul 17 '19 at 09:08
  • Hey .. so what happens if you remove the "cmd /c start" from the beginning, leaving only the XeLaTeX command? does it work? I think this cmd /c start it is causing the parallel – Brother Jul 17 '19 at 09:47

2 Answers2

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();
    }
Brother
  • 2,150
  • 1
  • 20
  • 24
  • 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 '19 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 '19 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.

davidbuzatto
  • 9,207
  • 1
  • 43
  • 50