如何运行命令';pdflatex&x27;在Mac上使用Java
我试着寻找答案,但没有一个对我有用 我正试图从Mac上的java应用程序在终端中运行pdflatex 在终端中,如果我键入:如何运行命令';pdflatex&x27;在Mac上使用Java,java,macos,terminal,command,pdflatex,Java,Macos,Terminal,Command,Pdflatex,我试着寻找答案,但没有一个对我有用 我正试图从Mac上的java应用程序在终端中运行pdflatex 在终端中,如果我键入: open -a FireFox http://www.yahoo.co.uk 它会在我的FireFox浏览器中打开yahoo.co.uk 或 它处理文件 在我的Java代码中,我键入: open -a FireFox http://www.yahoo.co.uk' 它会在我的FireFox浏览器中打开yahoo.co.uk 或 我犯了一个错误 代码如下: public
open -a FireFox http://www.yahoo.co.uk
它会在我的FireFox浏览器中打开yahoo.co.uk
或
它处理文件
在我的Java代码中,我键入:
open -a FireFox http://www.yahoo.co.uk'
它会在我的FireFox浏览器中打开yahoo.co.uk
或
我犯了一个错误
代码如下:
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);
}
}
以下是错误:
发生异常-我知道的是:java.io.IOException:无法运行程序“pdflatex”:错误=2,在
java.lang.ProcessBuilder.start(ProcessBuilder.java:460)位于
java.lang.Runtime.exec(Runtime.java:593)位于
java.lang.Runtime.exec(Runtime.java:431)位于
java.lang.Runtime.exec(Runtime.java:328)位于
test.JavaRunCommand.main(JavaRunCommand.java:28)
原因:java.io.IOException:error=2,在
位于
java.lang.UNIXProcess.(UNIXProcess.java:53)位于
java.lang.ProcessImpl.start(ProcessImpl.java:91)位于
java.lang.ProcessBuilder.start(ProcessBuilder.java:453)。。。4更多
我尝试过JProc,因为它是另一篇文章中的解决方案,但它仍然有一个类似的错误: 线程“main”org.buildobjects.process.StartupException中的异常:
无法启动进程“pdflatex x.tex”
位于org.buildobjects.process.Proc.(Proc.java:46)的
org.buildobjects.process.ProcBuilder.run(ProcBuilder.java:111)位于
test.JavaRunCommand.main(JavaRunCommand.java:20)
原因:java.io.IOException:
无法运行程序“pdflatex x.tex”:错误=2,在
java.lang.ProcessBuilder.start(ProcessBuilder.java:460)位于
java.lang.Runtime.exec(Runtime.java:593)位于
org.buildobjects.process.Proc.(Proc.java:43)。。。还有2个
原因:java.io.IOException:error=2,在
位于
java.lang.UNIXProcess.(UNIXProcess.java:53)位于
java.lang.ProcessImpl.start(ProcessImpl.java:91)位于
java.lang.ProcessBuilder.start(ProcessBuilder.java:453)。。。4更多
感谢@alf:
打印
which pdflatex
并使用生成的完整路径非常有效。尝试在命令行中打印which pdflatex
,然后将Java代码中的pdflatex
替换为其全名。谢谢@alf!成功了!但是现在我想打开生成的pdf文件。如何调用pdflatex并一次性打开生成的pdf文件?我已经完成了-我编写了一个批处理文件并执行了它。谢谢你的帮助!:)
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);
}
}