ujagujuma
ujagujuma

Reputation: 1

Issue with running a command in java

Im trying to use the terminal from java to convert a tex file to pdf:

...
        Process pr = Runtime.getRuntime().exec("pdflatex docu.tex") ;
        BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = "";
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
...

and I get this error :

Exception in thread "main" java.io.IOException: Cannot run program "pdflatex": error=2, No such file or directory

I have the docu.tex file in the same package as the above. When I input the command into the terminal directly it works fine and the pdf is made.

Thanks

Upvotes: 0

Views: 82

Answers (2)

ujagujuma
ujagujuma

Reputation: 1

Turns out all I had to do was find the explicit path to pdflatex(by typing "which pdflatex" in the terminal) like @PM 77-1 said, and replacing it with "pdflatex

Upvotes: 0

Joe
Joe

Reputation: 1342

You need to specify what Runtime#exec is going to use to execute your command, you can use bash like so, this should make your command execute on mac & linux (unix based) systems with bash available.

  final String[] executionStrings = new String[]{"/bin/bash", "-c", "pdflatex docu.tex"};
  Process p = Runtime.getRuntime().exec(executionStrings);

Upvotes: 2

Related Questions