0

I'm trying to run MixTex from a Java program, using the runtime.exec method. The code is

    try {
        Process process = Runtime.getRuntime().exec("miktex-pdf[--output-directory=(directory)][TestFile.tex]");
    } catch (IOException ex) {
    Logger.getLogger(CitationMachine.class.getName()).log(Level.SEVERE, null, ex);}

I'm getting an error saying that they can't find the program. I ve read Run .exe file in Java from file location but don't know how to adapt this to include the options and specify the tex file to be compiled. The documentation for latex command line I want to use is found at https://docs.miktex.org/manual/pdftex.html. Full documentation for other commands is found https://mirrors.sorengard.com/ctan/systems/win32/miktex/doc/2.9/miktex.pdf.

I want to specify the output directory of the file, the directory where the .tex file is located, and the name of the tex file

What am I doing wrong? Can you tell me how to fix my code or what would be a "better" way to run compile a tex document as part of a java program?

I know Apache POI exists, but I don't know how to use it and I'm already familiar with LaTex and know that I should be able to compile a pdf that way.

  • 1
    The program is *called* "miktex-pdf[--output-directory=(directory)][TestFile.tex]"??? That seems very suspicious.. recheck/share the documentation that shows the posted syntax is valid. – user2864740 Mar 23 '18 at 03:14
  • Can that command work from the command line? There doesn't appear to be spaces between the command and the parameters. I would, strongly, recommend using `ProcessBuilder` as it handles the parameters better – MadProgrammer Mar 23 '18 at 03:15
  • @MadProgrammer I'll try that – bme-programmer Mar 23 '18 at 03:24
  • 1
    @user2864740 I provided the documentation above. Here it is agin: https://docs.miktex.org/manual/pdftex.html I believe I did use correct syntax – bme-programmer Mar 23 '18 at 03:25
  • 2
    @trumpisterrible Haha. That's the confusion. The `[]`s in the documentation mean the arguments are *optional* - however, they are *not* meant to be included during invocation. Compare with `"miktex-pdftex --output-directory=some_dir TestFile.tex"` (it should be possible to run this successfully *from the command line*, without the quotes of course). Also, see @MadProgrammger's comment. I also changed `miktex-pdf` to `miktex-pdftex` per link. – user2864740 Mar 23 '18 at 03:27
  • @user2864740 This is the answer, so it should be posted as an answer. – Jim Garrison Mar 23 '18 at 04:58
  • @user2864740 Thanks – bme-programmer Mar 23 '18 at 12:26
  • @MadProgrammer Thanks – bme-programmer Mar 23 '18 at 12:26
  • @MadProgrammer I am now trying to use process builder using this code: – bme-programmer Mar 26 '18 at 00:50
  • @ MadProgrammer List processes = new ArrayList(); processes.add("miktex-pdftex --output-directory=E:\\IT Java Assignment\\CitationMachine Works_Cited.tex"); ProcessBuilder build = new ProcessBuilder(processes); – bme-programmer Mar 26 '18 at 00:51
  • @MadProgrammer this is based off of the same documentation for LaTex pdftex provided above – bme-programmer Mar 26 '18 at 00:52
  • @trumpisterrible Each command/parameter in the `List` should be it's own element, this way, you get over the issue if spaces in the command line/parameters – MadProgrammer Mar 26 '18 at 00:56
  • @MadProgrammer The commands are in their own lines in Netbeans. That is simply how it copied – bme-programmer Mar 26 '18 at 00:59
  • @MadProgrammer To have the processes in the Process builder run, do I type start(build); ? Or is there a different way I'm supposed to run all of the programs in Process builder? – bme-programmer Mar 26 '18 at 01:02
  • @trumpisterrible `processes.add("miktex-pdftex --output-directory=E:\\IT Java Assignment\\CitationMachine Works_Cited.tex")` is placing the command and all the parameters into a single element – MadProgrammer Mar 26 '18 at 01:18
  • @MadProgrammer What is the significance of putting the command and all of its parameters into a single element? This isn't answering my wuestion of how to get 'miktex-pdftex --output-directory...' to run. How do I do this? I can't find any examples in English on youtube – bme-programmer Mar 26 '18 at 01:25
  • @trumpisterrible As I said, using `ProcessBuilder` and placing each command/parameter into the command `List` as separate elements removes the need to deal with spaces in individual parameters (like paths). `ProcessBuilder` also provide additional configuration which can make executing external process simpler – MadProgrammer Mar 26 '18 at 02:07
  • @MadProgrammer The processes in the process builder won't run after I've added them to it. I;ve started a new question at https://stackoverflow.com/questions/49483015/ive-already-created-a-process-builder-how-do-i-run-all-of-the-programs-in-the . Since you were teh person who reccomended Process Builder to me (and understand it well) It would be appreciated if you can continue helping me get the process to run. So please reply on the new question's page – bme-programmer Mar 26 '18 at 02:18

0 Answers0