Reputation: 403
I am currently working on a java project which automatically generates texts files with LaTeX source code. These files are written to the hard drive at a know directory. Instead of having to manually compile the LaTeX source into a pdf myself, I want to use Runtime.getRunTime().exec()
.
I downloaded and installed BasicTeX. I do know that it is working properly. I can call the following in a new terminal window and the pdf is properly generated with no errors. Like this:
Kyles-MacBook-Pro:~ kylekrol$ cd Documents/folder/Report
Kyles-MacBook-Pro:Report kylekrol$ pdflatex latexDocument.txt
So I simply tried to use the following code before my program closed to compile the pdf:
private static void compileLatexMac(String path) {
String s = null;
try {
System.out.println("cd " + path.substring(0, path.length() - 1) + " && pdflatex latexDocument.txt");
Process p = Runtime.getRuntime().exec("cd " + path.substring(0, path.length() - 1) + " && pdflatex latexDocument.txt");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("outputs:");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("errors:");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
When I run this all I get back is the following without a pdf or a log file.
cd /Users/kylekrol/Documents/folder/Report && pdflatex latexDocument.txt
outputs:
errors:
I am not really sure what to make of this or why it isn't working - especially because I can call cd /Users/kylekrol/Documents/folder/Report && pdflatex latexDocument.txt
from Terminal and it runs as desired.
If someone could point out what I'm missing here that'd be great. Thanks!
I ended up running into more issues using the three argument exec()
command when running the jar file by double clicking on it. I fixed this by calling the following command which uses an extra pdflatex
argument and only absolute paths.
Runtime.getRuntime().exec("pdflatex -output-directory=" + path.substring(0, path.length() - 1) + " " + path + "latexDocument.txt");
Upvotes: 0
Views: 267
Reputation: 75575
The operator &&
is expanded by the shell. Runtime.exec
does not expand this operator and just attempts to executes the cd
command with the remainder of the command line as arguments. Thus, your pdflatex
command is never run.
If you want to run pdflatex
with a particular working directory, use the three-argument version of Runtime.exec
.
Upvotes: 1