本文介绍了java.io.IOException:无法运行程序"...":java.io.IOException:error = 2,没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从Java执行外部程序(使用libreoffice将 fodt 文件转换为 pdf 时,确实会发生这种情况)我知道程序所需的精确命令行:
I need to execute an external program from Java(to convert a fodt file to pdf using libreoffice, it so happens)I know the precise command-line I need for the program:
/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir /home/develop/tomcat/mf/ROOT/private/docs/0/ /home/develop/tomcat/mf/ROOT/private/docs/0/35_invoice.fodt
,并且在命令行中可以完美运行.但是使用 ProcessBuilder
and that works perfectly from the command line.But it does not work in Java using a ProcessBuilder
:
java.io.IOException: Cannot run program "/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir /home/develop/tomcat/mf/ROOT/private/docs/0 /home/develop/tomcat/mf/ROOT/private/docs/0/35_invoice.fodt": java.io.IOException: error=2, No such file or directory
我尝试了一些不同的方法,但没有成功.这是最后一次测试的示例
I tried some different approaches without success.Here is a sample of the last test
List<String> command = new ArrayList<String>();
command.add("/usr/bin/libreoffice");
command.add("--headless");
command.add("--convert-to pdf:'writer_pdf_Export' --outdir " + getDestinationDirectory(order) + " " + getInvoiceFilename() + ".fodt");
ProcessBuilder builder = new ProcessBuilder(command);
Process process = null;
try {
process = builder.start();
} catch (IOException ex) {
Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
}
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Program terminated!");
推荐答案
尝试一下(保持简单)...
Try this (keep it simple) ...
Process p = Runtime.getRuntime().exec("/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir "+ getDestinationDirectory(order)+" "+getInvoiceFilename()+".fodt");
完全...
Process process = null;
try {
process = Runtime.getRuntime().exec("/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir "+ getDestinationDirectory(order)+" "+getInvoiceFilename()+".fodt");
} catch (IOException ex) {
Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
Logger.getLogger(Documents.class.getName()).log(Level.SEVERE, null, ex);
}
br.close();
System.out.println("Program terminated!");
这篇关于java.io.IOException:无法运行程序"...":java.io.IOException:error = 2,没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!