Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #21327 > unrolled thread

problem with multiword args and Runtime.exec()

Started by"Aryeh M. Friedman" <Aryeh.Friedman@gmail.com>
First post2013-01-11 08:33 -0800
Last post2013-01-11 21:36 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  problem with multiword args and Runtime.exec() "Aryeh M. Friedman" <Aryeh.Friedman@gmail.com> - 2013-01-11 08:33 -0800
    Re: problem with multiword args and Runtime.exec() Lew <lewbloch@gmail.com> - 2013-01-11 11:07 -0800
    Re: problem with multiword args and Runtime.exec() FredK <fred.l.kleinschmidt@gmail.com> - 2013-01-11 11:41 -0800
      Re: problem with multiword args and Runtime.exec() FredK <fred.l.kleinschmidt@gmail.com> - 2013-01-11 11:44 -0800
    Re: problem with multiword args and Runtime.exec() "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2013-01-11 15:27 -0600
    Re: problem with multiword args and Runtime.exec() Martin Gregorie <martin@address-in-sig.invalid> - 2013-01-11 21:36 +0000

#21327 — problem with multiword args and Runtime.exec()

From"Aryeh M. Friedman" <Aryeh.Friedman@gmail.com>
Date2013-01-11 08:33 -0800
Subjectproblem with multiword args and Runtime.exec()
Message-ID<05a24a02-1ac3-4866-ba2b-20c488793b96@googlegroups.com>
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 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

[toc] | [next] | [standalone]


#21333

FromLew <lewbloch@gmail.com>
Date2013-01-11 11:07 -0800
Message-ID<d9d40450-ebd7-47ae-be3b-fdc1f245f328@googlegroups.com>
In reply to#21327
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 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

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.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#21334

FromFredK <fred.l.kleinschmidt@gmail.com>
Date2013-01-11 11:41 -0800
Message-ID<4600dcb5-1819-4e5b-aeae-70162d97144d@googlegroups.com>
In reply to#21327
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 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

[toc] | [prev] | [next] | [standalone]


#21335

FromFredK <fred.l.kleinschmidt@gmail.com>
Date2013-01-11 11:44 -0800
Message-ID<00bca6a0-7ce5-4e0c-8374-e3a8b5bed32d@googlegroups.com>
In reply to#21334
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 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 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 + "'";

[toc] | [prev] | [next] | [standalone]


#21336

From"A. Bolmarcich" <aggedor@earl-grey.cloud9.net>
Date2013-01-11 15:27 -0600
Message-ID<slrnkf10uv.uai.aggedor@earl-grey.cloud9.net>
In reply to#21327
On 2013-01-11, Aryeh M. Friedman <Aryeh.Friedman@gmail.com> 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 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

Did you try Runtime.exec(String[]), as in the following? 

  exec(new String[] {
      "rdiff-backup",
      "--remote-schema",
      "ssh -C -p9222 %s rdiff-backup --server",
      "username@remoteserver::/path_to/filestobackup",
      "/path_to/backedupfiles"
  })

[toc] | [prev] | [next] | [standalone]


#21337

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2013-01-11 21:36 +0000
Message-ID<kcq0k3$o1c$1@localhost.localdomain>
In reply to#21327
On Fri, 11 Jan 2013 08:33:39 -0800, 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 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

Something like 

Runtime r = new Runtime();
String  sc = String.format("ssh -C -p9222 %s rdiff-backup -- server",
                           hostName);
r.exec("rdiff-backup",
       "--remote-schema",
       sc,
       "username@remoteserver::/path_to/filestobackup " +
       "/path_to/backedupfiles");

ought to work.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web