problem with multiword args and Runtime.exec()

A

Aryeh M. Friedman

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
 
L

Lew

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/api/java/lang/Runtime.html#exec(java.lang.String[])
and its output.
 
F

FredK

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 );
 
F

FredK

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top