[英]java.io.IOException: Cannot run program “/usr/bin/sh”: java.io.IOException: error=2, No such file or directory
[英]java.io.IOException: Cannot run program “…”: java.io.IOException: error=2, No such file or directory
我需要從 Java 執行一個外部程序(使用 libreoffice 將fodt文件轉換為pdf ,它確實發生了)我知道我需要該程序的精確命令行:
/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
Java 中不起作用:
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
我嘗試了一些不同的方法但沒有成功。 這是上次測試的示例
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!");
ProcessBuilder
構造函數要求外部程序的每個參數都是獨立的(以數組或String
List
的形式)。 你得到的第一條異常消息,
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"
並沒有抱怨它可以找到一個名為/usr/bin/libreoffice
。 它抱怨它找不到一個名字很長很奇特的程序“ /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
",因為您將參數連接成一個String
。
代替
command.add("--convert-to pdf:'writer_pdf_Export' --outdir " + getDestinationDirectory(order) + " " + getInvoiceFilename() + ".fodt")
諸如此類,將每個參數拆分為自己對List.add
的調用
command.add("--convert-to");
command.add("pdf:writer_pdf_Export");
command.add("--outdir");
command.add(getDestinationDirectory(order).toString());
command.add(getInvoiceFilename() + ".fodt");
請注意,“writer_pdf_Export”周圍沒有撇號,因為它們是 shell 元字符,並且在構建數組以傳遞給沒有中間 shell 的exec
不需要。
試試這個(保持簡單)...
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!");
我已經嘗試了這個線程中提出的所有解決方案,但它不起作用。
在我的應用程序(在 linux 中使用 TOMCAT 的 java webapp)中,它只能創建一個 shell 腳本並執行該腳本。 但是你必須在腳本中放置絕對路徑,如果沒有,它不起作用($HOME 不起作用)。 此外,您可以向它傳遞參數。
例子:
Runtime.getRuntime().exec("/home/user/myscript.sh param1");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.