从java执行的rsync返回文件未找到错误

时间:2013-05-16 18:59:07

标签: java ssh rsync apache-commons-exec

我正在尝试使用Apache Commons Exec库执行rsync命令(本地atm测试)并收到错误消息,而在终端中执行相同的命令则没有问题。

这是我想要执行的命令:

rsync -zve "ssh -i /home/user/.ssh/testkey" /home/user/playground/src/HNI_0084.JPG user@localhost:/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, user@localhost:/home/user/playground/dst/}

完全无能为力rsync为什么会抱怨这个调用,因为包含空格的参数的引用是由库处理的,而实际的rsync调用应该看起来与我在上面发布的行完全一样。

有什么想法吗? :/

1 个答案:

答案 0 :(得分:0)

从java代码执行时,rsync可能找不到ssh。尝试使用ssh exectable的完整路径,而不是仅仅传递命令。