• Post Reply Bookmark Topic Watch Topic
  • New Topic

del command with Runtime.exec()  RSS feed

 
prachi jahagirdar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
I was trying to run the DOS command 'del' using Runtime.exec().
Other commands work properly.
I have the files in respective folders which are to be deleted.
My code look like this
String command="del c:\test\*.txt ";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
Output is
java.io.IOException: CreateProcess: del c:\abc\test\*.txt error=0
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:67)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:566)
at java.lang.Runtime.exec(Runtime.java:428)
at java.lang.Runtime.exec(Runtime.java:364)
at java.lang.Runtime.exec(Runtime.java:326)
at CreatePatch.runcommand(CreatePatch.java:161)
at CreatePatch.getVector(CreatePatch.java:144)
at CreatePatch.main(CreatePatch.java:26)
 
Nischal Topno
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
[code]
String command="cmd /c del c:\test\*.txt ";
[\code]
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prachi jahagirdar:
hello,
I was trying to run the DOS command 'del' using Runtime.exec().
Other commands work properly.
I have the files in respective folders which are to be deleted.
My code look like this
String command="del c:\test\*.txt ";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
Output is
java.io.IOException: CreateProcess: del c:\abc\test\*.txt error=0
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:67)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:566)
at java.lang.Runtime.exec(Runtime.java:428)
at java.lang.Runtime.exec(Runtime.java:364)
at java.lang.Runtime.exec(Runtime.java:326)
at CreatePatch.runcommand(CreatePatch.java:161)
at CreatePatch.getVector(CreatePatch.java:144)
at CreatePatch.main(CreatePatch.java:26)

This is because del is a built in command of the command.com/cmd.exe shell. To execute such commands you have to invoke cmd.exe or command.com and pass it the del command as the argument.
Read this article.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!