gpt4 book ai didi

java - 如何在 Mac 上用 Java 运行命令 'pdflatex'

转载 作者:搜寻专家 更新时间:2023-10-31 20:30:20 28 4
gpt4 key购买 nike

我已经尝试寻找答案,但没有一个对我有用。

我正在尝试从我的 Mac 上的 java 应用程序在终端中运行 pdflatex。

如果我在终端中输入:

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

它会在我的 FireFox 浏览器中打开 yahoo.co.uk

pdflatex x.tex

它处理文件

在我的 Java 代码中,我输入:

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

它会在我的 FireFox 浏览器中打开 yahoo.co.uk

pdflatex x.tex

我得到一个错误。

代码如下:

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);
}
}

这是错误:

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

我试过 JProc,因为它是另一个帖子的解决方案,但它仍然有类似的错误:

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

最佳答案

感谢@alf:

打印 which pdflatex 并使用生成的完整路径效果很好。

关于java - 如何在 Mac 上用 Java 运行命令 'pdflatex',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8383964/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com