繁体   English   中英

在java中运行命令的问题

[英]Issue with running a command in java

我正在尝试使用 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);
        }
...

我收到此错误:

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

我有与上述相同的包中的 docu.tex 文件。 当我直接将命令输入终端时,它工作正常并且生成了 pdf。

谢谢

您需要指定 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