2013-05-16 91 views
4

我想執行一個rsync命令(atm測試本地)使用Apache Commons Exec庫和我得到一個錯誤消息,而在終端執行相同的命令顯示沒有問題。從java執行rsync返回與文件未找到錯誤

這是我想執行的命令:

rsync -zve "ssh -i /home/user/.ssh/testkey" /home/user/playground/src/HNI_0084.JPG [email protected]:/home/user/playground/dst 

這是錯誤消息我得到的,當我在我的Java類執行命令:

rsync: Failed to exec ssh -i /home/user/.ssh/testkey: No such file or directory (2) 
rsync error: error in IPC code (code 14) at pipe.c(84) [sender=3.0.9] 
rsync: connection unexpectedly closed (0 bytes received so far) [sender] 
rsync error: error in IPC code (code 14) at io.c(605) [sender=3.0.9] 
org.apache.commons.exec.ExecuteException: Process exited with an error: 14 (Exit value: 14) 
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:377) 
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:160) 
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:147) 
    at LocalExecutor.execute(LocalExecutor.java:21) 

所以,是的,這是我的Java類執行本地命令:

import java.io.IOException; 

import org.apache.commons.exec.CommandLine; 
import org.apache.commons.exec.DefaultExecutor; 
import org.apache.commons.exec.ExecuteWatchdog; 

public class LocalExecutor { 

    public int execute(String command, String[] args) { 
     CommandLine cl = new CommandLine(command); 
     cl.addArguments(args); 
     System.out.println(cl.toString()); 

     DefaultExecutor executor = new DefaultExecutor(); 
     ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); 
     executor.setWatchdog(watchdog); 
     int exitValue = 0; 
     try { 
      exitValue = executor.execute(cl); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return exitValue; 
    } 

} 

最後,參數數組傳遞給程序是這樣的:

{-zve, ssh -i /home/user/.ssh/testkey, /home/user/playground/src/HNI_0084.JPG, [email protected]:/home/user/playground/dst/} 

完全無能,爲什麼會rsync的抱怨電話,因爲包含空格被庫和實際rsync的呼叫處理參數的報價則應該看起來就像我上面貼線。

任何想法? :/

回答

0

rsync從java代碼執行時可能找不到ssh。嘗試使用ssh可執行文件的完整路徑,而不是僅傳遞命令。

相關問題