简体   繁体   English

在java中运行命令的问题

[英]Issue with running a command in java

Im trying to use the terminal from java to convert a tex file to pdf:我正在尝试使用 java 中的终端将 tex 文件转换为 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.我有与上述相同的包中的 docu.tex 文件。 When I input the command into the terminal directly it works fine and the pdf is made.当我直接将命令输入终端时,它工作正常并且生成了 pdf。

Thanks谢谢

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.您需要指定 Runtime#exec 将用于执行您的命令的内容,您可以像这样使用 bash,这应该使您的命令在 mac 和 linux(基于 unix 的)系统上执行,并且 bash 可用。

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

原来我所要做的就是找到 pdflatex 的显式路径(通过在终端中键入“which pdflatex”),就像@PM 77-1 所说的那样,并将其替换为“pdflatex”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM