4

I am trying to execute a rsync command (atm testing locally) using Apache Commons Exec library and I get an error message, while executing the same command in terminal shows no problems.

This is the command I would like to execute:

rsync -zve "ssh -i /home/user/.ssh/testkey" /home/user/playground/src/HNI_0084.JPG user@localhost:/home/user/playground/dst

And this is the error message I get when I execute the command in my Java class:

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)

So yeah, this is my java class for executing local commands:

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;
    }

}

And lastly, the arguments array passed to the program is this:

{-zve, ssh -i /home/user/.ssh/testkey, /home/user/playground/src/HNI_0084.JPG, user@localhost:/home/user/playground/dst/}

Completely clueless why would rsync complain about the call, since the quoting of the arguments that contain spaces is handled by the library and the actual rsync call should then look exactly like the line I posted above.

Any ideas? :/

Esch
  • 79
  • 1
  • 5

1 Answers1

0

It is possible that rsync doesn't find ssh when executed from your java code. Try to use the full path to the ssh exectable instead of just passing the command.

Guy Bouallet
  • 2,099
  • 11
  • 16