This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Runtime.exec problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.exec problem" Watch "Runtime.exec problem" New topic
Author

Runtime.exec problem

boli srivastava
Greenhorn

Joined: Jun 29, 2008
Posts: 3
I have to copy a file using runtime.exec.
I am using the following code , then it is working fine:
String command= "cmd.exe start /c copy"+ " " + "..\\src\\Source.txt" + " " + "..\\src\\destination.txt" ;
System.out.println(command);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( command );
int i = proc.waitFor();
the files are copied successfully.
The proc.waitFor() returns zero which is normal termination .

However, when I use this code :

String from ="..\\src\\Source.txt" ;
String to="..\\src\\destination.txt";
String command= "cmd.exe start /c copy" + " " + from + " " + to ;
System.out.println(command);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( command );
int i = proc.waitFor();
The files are not copied at all and proc.waitFor() returns 1 which is not normal.

Could you expalin if runtime.exec handles String variables differently.The command is same in both the cases as I am printing it out on console.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

1) UseCodeTags
2) Read the Javaworld article "When Runtime.exec() won't"
3) What is printed to the Process' error stream? Call proc.getErrorStream() and read all data from it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.exec problem
 
Similar Threads
Running unix command using java shows error
Controlling Windows Services from a Java App
Copying files
Runtime.exec("c.bat")
how can i use shell32 in my java program