problem with multiword args and Runtime.exec()

Discussion in 'Java' started by Aryeh M. Friedman, Jan 11, 2013.

  1. 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
     
    Aryeh M. Friedman, Jan 11, 2013
    #1
    1. Advertising

  2. Aryeh M. Friedman

    Lew Guest

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

    --
    Lew
     
    Lew, Jan 11, 2013
    #2
    1. Advertising

  3. Aryeh M. Friedman

    FredK Guest

    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, Jan 11, 2013
    #3
  4. Aryeh M. Friedman

    FredK Guest

    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 + "'";
     
    FredK, Jan 11, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Varn
    Replies:
    2
    Views:
    658
    Ken Varn
    Jun 22, 2005
  2. Bob Connell
    Replies:
    4
    Views:
    1,376
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,198
    Gordon Beaton
    May 22, 2006
  4. Replies:
    3
    Views:
    540
    David Eppstein
    Sep 17, 2003
  5. er
    Replies:
    2
    Views:
    544
Loading...

Share This Page