2013-05-10 345 views
5

我需要將fodt文件轉換爲pdf。試圖使用各種轉換器浪費了數天後,我意識到使用libreoffice可以做到這一點。java.io.IOException:無法運行程序「...」:java.io.IOException:error = 2,沒有此類文件或目錄

/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工作...

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 

我跟很多的HOWTO和不同的方法,但結果都是一樣的。

命令

"/usr/bin/libreoffice --headless --convert-to pdf:'writer_pdf_Export' --outdir "+ getDestinationDirectory(order)+" "+getInvoiceFilename()+".fodt"); 

我嘗試了所有在一個字符串和分裂成的String []過。在後一種情況下它抱怨的參數:

Unknown option: --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 

這裏是最後的測試樣品

 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!"); 
+1

當文件或目錄您嘗試移動OU做的東西並不存在此錯誤是典型的。你的命令很可能是正確的,但是,你的文件或路徑沒有到達另一端(例如路徑)。 – 2013-05-10 12:51:56

回答

1

試試這個(保持簡單)...

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!"); 
+0

這是第一次測試:*沒有錯誤,沒有輸出,最重要的是......沒有PDF * – Azathoth 2013-05-10 12:58:58

+0

它從命令行工作?我發現這個https://bugs.freedesktop.org/show_bug.cgi?id=44486 – xagyg 2013-05-10 13:02:50

+0

當你讀取進程'InputStream'和進程'ErrorStream'時,你會得到同樣的錯誤嗎?請參閱'getInputStream'和'getErrorStream'。 – xagyg 2013-05-10 13:05:49

4

而不是

command.add("--convert-to pdf:'writer_pdf_Export' --outdir " + getDestinationDirectory(order) + " " + getInvoiceFilename() + ".fodt") 

嘗試拆分每個ARGV成其自己致電加

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」周圍沒有撇號,因爲這些是外殼元字符,並且當您構建數組傳遞到exec而沒有中介外殼時不需要。

+0

也試過:*未知選項:--convert-to pdf:'writer_pdf_Export'* – Azathoth 2013-05-10 12:54:10

+1

@Azathoth,擺脫你的撇號。這些是libreoffice可執行文件不應該看到的shell元字符。 – 2013-05-10 13:01:04

+0

完成:*未知選項:--convert-to pdf:writer_pdf_Export * – Azathoth 2013-05-10 13:02:24

0

我試圖在這個線程建議每個解決方案,這是行不通的。

在我的應用程序(java webapp在Linux中使用TOMCAT)它只能用來創建一個shell腳本並執行腳本。但是你必須在腳本中放置絕對路徑,否則它不起作用($ HOME不起作用)。此外,你可以傳遞它的參數。

例子:

Runtime.getRuntime().exec("/home/user/myscript.sh param1"); 
相關問題