Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

runtime.exec() How to copy all files ?

Thread view: 
k4 - 09 Aug 2007 13:46 GMT
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
Gordon Beaton - 09 Aug 2007 15:36 GMT
> 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

--
Joe Attardi - 09 Aug 2007 15:40 GMT
>     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?

Signature

Joe Attardi
jattardi@gmail.com

Arne Vajhøj - 12 Aug 2007 04:48 GMT
>>     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
Joe Attardi - 12 Aug 2007 16:53 GMT
On Aug 11, 11:48 pm, Arne Vajh?j <a...@vajhoej.dk> 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 :)
Nigel Wade - 09 Aug 2007 15:45 GMT
> Witam
>
[quoted text clipped - 30 lines]
> 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/"};

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.