0

My question is two parts.

First part:

I am writing a series of latex files, and I would like to compile them.

The command works if I type it manually. so the syntax should be correct. But when I run it from java application, it won't compile.

This code, in Java creates a latex file correctly--as expected.

File latexFile = fileChooser.showSaveDialog(window.stage);
if (latexFile != null) {
    try {
        writeContentToFile(content, latexFile);
    } catch (IOException ex) {
        Logger.getLogger(DocumentViewPort.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I can compile the generated file in the command line if I type pdflatex and then add the file path and get a PDF file.

but it does not seem to work when doing it from java itself like in here:

try {
    Runtime.getRuntime().exec( "pdflatex " + latexFile.getPath());
} catch (IOException ex) {
    Logger.getLogger(FileAndContentCreation.class.getName()).log(Level.SEVERE, null, ex);
}

any ideas why? I tried to read online on many places, I can't find out what part is the problematic one. Any help would be greatly appreciated. Thanks.

Second part

My second part of the question is changing the directory, I am using:

Runtime.getRuntime().exec( "cd " + myNewdirectory.getPath());

But it fails with an error=2

Any ideas how to change the current directory?

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
melkhaldi
  • 899
  • 2
  • 19
  • 40
  • Does the command `pdflatex` need any other input ? Why do you need to change the directory ? – ItachiUchiha Jun 13 '14 at 06:40
  • Hi, no it does not need other output. The same command works on the same file if I run it manually from the command line. for the directory, If i don't change it, the pdf gets created in the user folder, but I want it to do in a specific folder. when I do cd mydir then pdflatex myfile.tex from the command line, it all works. but from java's runtime, neither one works. – melkhaldi Jun 13 '14 at 06:43
  • Check what `"pdflatex "+latexFile.getPath()` is evaluating to – James_D Jun 13 '14 at 12:07
  • Also, instead of changing directory, try (something like) `Runtime.getRuntime().exec("pdflatex -output-directory "+myNewDirectory.getPath()+" "+latexFile.getPath());` Check the documentation for `pdflatex`, but iirc that is the correct option. – James_D Jun 13 '14 at 12:56
  • Thanks the change directory worked! and the compile to PDF worked.! The only thing I found strange is that compiling command won't work if the folder name had white space in it... any ideas why? – melkhaldi Jun 15 '14 at 14:01
  • /can you add your answer as an "Answer" so that the question gets closed :) ? – melkhaldi Jun 15 '14 at 14:01
  • For the first part, you have to capture the std out and std error, or else it won't work. See [a similar post here](http://stackoverflow.com/questions/33298548/pdflatex-run-crashes-when-executed-from-java-on-windows) – trudesagen Dec 19 '16 at 11:30

0 Answers0