Executing Command line commands from java - problem with deleting a file
The Command line execution function works fine when I try to run a program by this code ( there is more of it but this shows what I am using )
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(command);
int exitVal = p.waitFor();
BUT when I try to make java delete a file by saying command="del file.txt"
I use Windows by the way
It does not do it!!! I dont know why, here is the exception I get :
java.io.IOException: Cannot run program "del": CreateProcess error=2, The system cannot find the file specified
Basically there is no file called "del" yes I get it... But why doesn't it take the command as whole? How can I make it delete a file?
The method delete in java.io.File
It is better to use the method delete() in the class File to delete a file, rather open creating a separate process to delete the file,
Command string when deleting file as process
The command="del file.txt" should be "cmd /c del file.txt"