我正在尝试使用 Runtime.exec() 将带空格的文件名传递给 java 应用程序内的 pdflatex。
该命令在终端上执行良好
pdflatex --halt-on-error "/home/jody/test 1.tex"
即生成pdf,返回码为0。
当像这样传递给 exec() 时,相同的命令将不起作用(即返回代码 1 而不是 0;没有创建 pdf)
myRuntime.exec("pdflatex --halt-on-error \"/home/jody/test 1.tex\"", null, null);
我该如何编写这样的命令才能使其工作? 谢谢 乔迪
最佳答案
我找到了解决方案:
而不是使用
public Process exec(String command,
String[] envp,
File dir)
其中整个调用包含在字符串“command”中, 我现在使用
public Process exec(String[] cmdarray,
String[] envp,
File dir)
其中调用被分成一个数组“cmdarray”,其中包含命令(即“pdflatex”)及其参数作为元素。这样 pdflatex 将充满空格的文件名理解为单个单词,并可以完成其工作。
关于java - pdflatex和java中带空格的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30326744/