runtime.exec() How to copy all files ?

K

k4

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
 
G

Gordon Beaton

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

--
 
J

Joe Attardi

k4 said:
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?
 
N

Nigel Wade

k4 said:
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/"};
 
G

Guest

Joe said:
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
 
J

Joe Attardi

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

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top