runtime.exec() How to copy all files ?

Discussion in 'Java' started by k4, Aug 9, 2007.

  1. k4

    k4 Guest

    Witam

    My class:

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;


    public class test {

    public static void main( String [] args ) {
    try{
    Runtime runtime = Runtime . getRuntime();

    String cmd ="/bin/cp /home/k/java/* /var/www/k/";
    System.out.println(cmd);
    Process process = runtime.exec(cmd);
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader bfr = new BufferedReader(isr);
    String line;

    while ( ( line = bfr . readLine() ) != null ) {
    System.out.println(line);
    }
    }catch(Exception e){
    }

    }
    }

    If I use "*" to copy all files, then command doesn't work why? If I try
    copy single file then everything is ok.
    String cmd ="/bin/cp /home/k/java/single.txt /var/www/k/";

    How to copy all files ? :/a
     
    k4, Aug 9, 2007
    #1
    1. Advertising

  2. On Thu, 09 Aug 2007 14:46:22 +0200, k4 wrote:
    > If I use "*" to copy all files, then command doesn't work why? If I
    > try copy single file then everything is ok. String cmd ="/bin/cp
    > /home/k/java/single.txt /var/www/k/";
    >
    > How to copy all files ? :/a


    "*" is a special character only to a command shell. The shell itself
    expands "*" before passing the resulting file list to the cp.

    Runtime.exec() does not use a shell to run the command, so cp sees a
    literal "*" instead of the file list.

    If you want to use shell features, run a shell:

    String[] cmd = {
    "/bin/sh",
    "-c",
    "/bin/cp /home/k/java/* /var/www/k/"
    };

    /gordon

    --
     
    Gordon Beaton, Aug 9, 2007
    #2
    1. Advertising

  3. k4

    Joe Attardi Guest

    k4 wrote:
    > String cmd ="/bin/cp /home/k/java/* /var/www/k/";

    Seems overkill to use Runtime.exec() just to copy files. Why not just
    use a Reader and a Writer?


    --
    Joe Attardi
     
    Joe Attardi, Aug 9, 2007
    #3
  4. k4

    Nigel Wade Guest

    k4 wrote:

    > Witam
    >
    > My class:
    >
    > import java.io.BufferedReader;
    > import java.io.InputStream;
    > import java.io.InputStreamReader;
    >
    >
    > public class test {
    >
    > public static void main( String [] args ) {
    > try{
    > Runtime runtime = Runtime . getRuntime();
    >
    > String cmd ="/bin/cp /home/k/java/* /var/www/k/";
    > System.out.println(cmd);
    > Process process = runtime.exec(cmd);
    > InputStream is = process.getInputStream();
    > InputStreamReader isr = new InputStreamReader(is);
    > BufferedReader bfr = new BufferedReader(isr);
    > String line;
    >
    > while ( ( line = bfr . readLine() ) != null ) {
    > System.out.println(line);
    > }
    > }catch(Exception e){
    > }
    >
    > }
    > }
    >
    > If I use "*" to copy all files, then command doesn't work why? If I try
    > copy single file then everything is ok.
    > String cmd ="/bin/cp /home/k/java/single.txt /var/www/k/";


    Because "*" is only special to the shell, and you are not invoking a shell. In
    your case the executable /bin/cp would try to copy a file called "*", and there
    isn't one.

    >
    > How to copy all files ? :/a


    Invoke a shell:

    String[] cmd ={"/bin/sh", "-c", "/bin/cp /home/k/java/* /var/www/k/"};

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Aug 9, 2007
    #4
  5. Joe Attardi wrote:
    > k4 wrote:
    >> String cmd ="/bin/cp /home/k/java/* /var/www/k/";

    > Seems overkill to use Runtime.exec() just to copy files. Why not just
    > use a Reader and a Writer?


    I would only use Reader & Writer if I knew all the files were
    text files.

    Byte oriented classes would be more general.

    Arne
     
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Aug 12, 2007
    #5
  6. k4

    Joe Attardi Guest

    On Aug 11, 11:48 pm, Arne Vajhøj <> wrote:
    > I would only use Reader & Writer if I knew all the files were
    > text files.
    >
    > Byte oriented classes would be more general.


    Whoops, I wasn't even thinking. I meant FileInputStream/
    FileOutputStream, not Reader/Writer.
    Thanks Arne :)
     
    Joe Attardi, Aug 12, 2007
    #6
    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. Dave Neuendorf
    Replies:
    5
    Views:
    6,753
    Anthony Borla
    Feb 25, 2005
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,155
    Gordon Beaton
    May 22, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,124
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Alex
    Replies:
    2
    Views:
    1,254
  5. Replies:
    26
    Views:
    2,140
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page