Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Runtime.exec problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic