Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   problem with multiword args and Runtime.exec() (http://www.velocityreviews.com/forums/t956430-problem-with-multiword-args-and-runtime-exec.html)

Aryeh M. Friedman 01-11-2013 04:33 PM

problem with multiword args and Runtime.exec()
 
No matter what version of Runtime.exec I call I can not make a arg that hasmultiple words in it (*NOT* multiple args but a single one) for example ifI need to issue the following command in unix I can not find a way to do the stuff in "'s as a single arg [the called program requires this... i.e. it *MUST NOT* be treated as different elements of String[] args if it was a java program]):

rdiff-backup --remote-schema "ssh -C -p9222 %s rdiff-backup --server" username@remoteserver::/path_to/filestobackup /path_to/backedupfiles

Lew 01-11-2013 07:07 PM

Re: problem with multiword args and Runtime.exec()
 
On Friday, January 11, 2013 8:33:39 AM UTC-8, Aryeh M. Friedman wrote:
> No matter what version of Runtime.exec I call I can not make a arg that has multiple words in it (*NOT* multiple args but a single one) for example if I need to issue the following command in unix I can not find a way to dothe stuff in "'s as a single arg [the called program requires this... i.e.it *MUST NOT* be treated as different elements of String[] args if it was a java program]):
>
>
>
> rdiff-backup --remote-schema "ssh -C -p9222 %s rdiff-backup --server" username@remoteserver::/path_to/filestobackup /path_to/backedupfiles


Please provide an SSCCE
http://sscce.org/
of your code using
http://docs.oracle.com/javase/7/docs...va.lang.String[])
and its output.

--
Lew

FredK 01-11-2013 07:41 PM

Re: problem with multiword args and Runtime.exec()
 
On Friday, January 11, 2013 8:33:39 AM UTC-8, Aryeh M. Friedman wrote:
> No matter what version of Runtime.exec I call I can not make a arg that has multiple words in it (*NOT* multiple args but a single one) for example if I need to issue the following command in unix I can not find a way to dothe stuff in "'s as a single arg [the called program requires this... i.e.it *MUST NOT* be treated as different elements of String[] args if it was a java program]):
>
>
>
> rdiff-backup --remote-schema "ssh -C -p9222 %s rdiff-backup --server" username@remoteserver::/path_to/filestobackup /path_to/backedupfiles


Your program gets the quotes string as a single argument.
If you want to pass that string as a single argument to something
like system(), you have to ensure that there are quotes around the
multi-word argument.

For example, assume arg1 contains the multiword value.
You cannot just say:
sprintf( buffer, "ls %s", arg1 );
system( buffer );

you have to use
sprintf( buffer, "ls '%s'", arg1 );
system( buffer );


--
Fred K



FredK 01-11-2013 07:44 PM

Re: problem with multiword args and Runtime.exec()
 
On Friday, January 11, 2013 11:41:00 AM UTC-8, FredK wrote:
> On Friday, January 11, 2013 8:33:39 AM UTC-8, Aryeh M. Friedman wrote:
>
> > No matter what version of Runtime.exec I call I can not make a arg thathas multiple words in it (*NOT* multiple args but a single one) for example if I need to issue the following command in unix I can not find a way to do the stuff in "'s as a single arg [the called program requires this... i.e. it *MUST NOT* be treated as different elements of String[] args if it was a java program]):

>
> >

>
> >

>
> >

>
> > rdiff-backup --remote-schema "ssh -C -p9222 %s rdiff-backup --server" username@remoteserver::/path_to/filestobackup /path_to/backedupfiles

>
>
>
> Your program gets the quotes string as a single argument.
>
> If you want to pass that string as a single argument to something
>
> like system(), you have to ensure that there are quotes around the
>
> multi-word argument.
>
>
>
> For example, assume arg1 contains the multiword value.
>
> You cannot just say:
>
> sprintf( buffer, "ls %s", arg1 );
>
> system( buffer );
>
>
>
> you have to use
>
> sprintf( buffer, "ls '%s'", arg1 );
>
> system( buffer );
>
>
>
>
>
> --
>
> Fred K


Oops - answered it as "C".
However, the same concept goes for Java
String buffer = "ls '" + arg1 + "'";


All times are GMT. The time now is 01:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.