Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the command 'pdflatex' in Java on Mac

I've tried searching for the answer but none are working for me.

I'm trying to run pdflatex in terminal from my java application on my Mac.

In Terminal if i type:

open -a FireFox http://www.yahoo.co.uk

it opens yahoo.co.uk in my FireFox browser

or

pdflatex x.tex

it processes the file

In my Java code I type:

open -a FireFox http://www.yahoo.co.uk'

it opens yahoo.co.uk in my FireFox browser

or

pdflatex x.tex

I get an error.

Here's the Code:

public static void main(String args[]) {

    String s = null;

    try {

        Process p = Runtime.getRuntime().exec("pdflatex x.tex");

         BufferedReader stdInput = new BufferedReader(new
         InputStreamReader(p.getInputStream()));

         BufferedReader stdError = new BufferedReader(new
         InputStreamReader(p.getErrorStream()));

         // read the output from the command
         System.out.println("Here is the standard output of the command:\n");
         while ((s = stdInput.readLine()) != null) {
         System.out.println(s);
         }

         // read any errors from the attempted command
         System.out.println("Here is the standard error of the command (if any):\n");
         while ((s = stdError.readLine()) != null) {
         System.out.println(s);
         }

         System.exit(0);
    } catch (Exception e) {
        System.out.println("exception happened - here's what I know: ");
        e.printStackTrace();
        System.exit(-1);
    }
}

Here's the error:

exception happened - here's what I know:
java.io.IOException: Cannot run program "pdflatex": error=2, No such file or directory at
java.lang.ProcessBuilder.start(ProcessBuilder.java:460) at
java.lang.Runtime.exec(Runtime.java:593) at
java.lang.Runtime.exec(Runtime.java:431) at
java.lang.Runtime.exec(Runtime.java:328) at
test.JavaRunCommand.main(JavaRunCommand.java:28)
Caused by: java.io.IOException: error=2, No such file or directory at
java.lang.UNIXProcess.forkAndExec(Native Method) at
java.lang.UNIXProcess.(UNIXProcess.java:53) at
java.lang.ProcessImpl.start(ProcessImpl.java:91) at
java.lang.ProcessBuilder.start(ProcessBuilder.java:453) ... 4 more

I have tried JProc as it was a solution from another post, but it still has a similar error:

Exception in thread "main" org.buildobjects.process.StartupException:
Could not startup process 'pdflatex x.tex '.
at org.buildobjects.process.Proc.(Proc.java:46) at
org.buildobjects.process.ProcBuilder.run(ProcBuilder.java:111) at
test.JavaRunCommand.main(JavaRunCommand.java:20)
Caused by: java.io.IOException:
Cannot run program "pdflatex x.tex": error=2, No such file or directory at
java.lang.ProcessBuilder.start(ProcessBuilder.java:460) at
java.lang.Runtime.exec(Runtime.java:593) at
org.buildobjects.process.Proc.(Proc.java:43) ... 2 more
Caused by: java.io.IOException: error=2, No such file or directory at
java.lang.UNIXProcess.forkAndExec(Native Method) at
java.lang.UNIXProcess.(UNIXProcess.java:53) at
java.lang.ProcessImpl.start(ProcessImpl.java:91) at
java.lang.ProcessBuilder.start(ProcessBuilder.java:453) ... 4 more

like image 359
Devan Somaia Avatar asked Dec 05 '11 10:12

Devan Somaia


1 Answers

thanks to @alf :

printing which pdflatex and using the resulting full path works perfectly.

like image 114
Devan Somaia Avatar answered Oct 02 '22 16:10

Devan Somaia