You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Tech Dev <te...@gmail.com> on 2015/08/26 04:34:03 UTC

[exec] - Simple Copy using Apache Commons

Hi,

I am trying to execute a copy command using Apache Commons Exec API. Below
is my effort :

        String initialCommand = "scp -i";
        String privateKey = "/Users/TD/.ssh/id_rsa";
        String currentFile = "/Users/TD/One.txt";
        String target = "root@my.server.com:";

        // Space is present in destination
        String destination="/Leo/Ta/San Diego";
        destination=destination.replaceAll(" ", "\\\\ ");

        String completeCommand = initialCommand + " " + privateKey + " " +
currentFile + " " + target + destination;
        System.out.println(completeCommand);

        CommandLine commandLine = new CommandLine(completeCommand);

        System.out.println(commandLine.toString());
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(0);
        executor.execute(commandLine);

Output :

> scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt root@my.server.com:/Leo/Ta/San\
Diego

> scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt root@my.server.com:/Leo/Ta/San/
Diego

I am noticing that backslash is getting updated and also my command is not
running with error "No such file or directory".

I am new to this library. Any suggestion would be appreciated.

Re: [exec] - Simple Copy using Apache Commons

Posted by sebb <se...@gmail.com>.
Don't build the command line yourself, let Exec do it:

http://commons.apache.org/proper/commons-exec/commandline.html#Building_the_Command_Line_Incrementally

On 26 August 2015 at 03:34, Tech Dev <te...@gmail.com> wrote:
> Hi,
>
> I am trying to execute a copy command using Apache Commons Exec API. Below
> is my effort :
>
>         String initialCommand = "scp -i";
>         String privateKey = "/Users/TD/.ssh/id_rsa";
>         String currentFile = "/Users/TD/One.txt";
>         String target = "root@my.server.com:";
>
>         // Space is present in destination
>         String destination="/Leo/Ta/San Diego";
>         destination=destination.replaceAll(" ", "\\\\ ");
>
>         String completeCommand = initialCommand + " " + privateKey + " " +
> currentFile + " " + target + destination;
>         System.out.println(completeCommand);
>
>         CommandLine commandLine = new CommandLine(completeCommand);
>
>         System.out.println(commandLine.toString());
>         DefaultExecutor executor = new DefaultExecutor();
>         executor.setExitValue(0);
>         executor.execute(commandLine);
>
> Output :
>
>> scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt root@my.server.com:/Leo/Ta/San\
> Diego
>
>> scp -i /Users/TD/.ssh/id_rsa /Users/TD/One.txt root@my.server.com:/Leo/Ta/San/
> Diego
>
> I am noticing that backslash is getting updated and also my command is not
> running with error "No such file or directory".
>
> I am new to this library. Any suggestion would be appreciated.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org