我正在尝试使用 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 调用应该看起来与我在上面发布的行完全相同。
有任何想法吗?:/