-1

The following command is working perfectly when I execute it from the terminal:

rm -f /home/folkman/Desktop/DimacsProba/*

However, I want to execute it with Java. I am using Netbeans and when I used an older version of Ubuntu, it worked:

     Runtime.getRuntime().exec("rm -f /home/folkman/Desktop/DimacsProba/*");

Now, with Ubuntu 13.04. nothing of the following is working:

 //1
   Runtime.getRuntime().exec("rm -f /home/folkman/Desktop/DimacsProba/*");

 //2
   Runtime.getRuntime().exec(new String[]{"rm" , "-f", "/home/folkman/Desktop/DimacsProba/*"});

 // 3
 ProcessBuilder pb= new ProcessBuilder("rm","/home/folkman/Desktop/DimacsProba/*");
 Process p=pb.start(); 

Maybe it is not because of the Ubuntu, but I really don't know. Please help! Thank you!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Why dont you delete files via the Java api? – Erik Ekman Jul 14 '13 at 22:41
  • There is nothing wrong with calling shell to delete files, however that way sacrifices portability. For better portability, and if it suits your use case, you may want to use Java file API to recursively delete files in a dir. –  Jul 15 '13 at 00:42
  • Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces (as in e.g.s 2 & 3). – Andrew Thompson Jul 15 '13 at 03:07

1 Answers1

6

Wild cards are a shell feature so if you use exec, they will be taken literally. If you have a file called * it will be deleted, but I suspect you wanted a shell here to expand the *

Try

"sh", "-c", "rm -f /home/folkman/Desktop/DimacsProba/*"
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130