ProcessBuilder giving a "File not found" exception when the file does exist

Matthew Salsamendi picture Matthew Salsamendi · Feb 8, 2013 · Viewed 10.8k times · Source

Working on an application that will run on a Linux web server to delete logs from a certain directory, however I keep getting a FileNotFound exception.

Here is the code:

public static void deleteLOG() {
    try {
        ProcessBuilder probuilder = new ProcessBuilder("find /home/root/multicraft/servers/ -name '*.log' -delete");
        probuilder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And the exception

java.io.IOException: Cannot run program "find . -name '*CoreData*' -delete" (in directory "/home/root/multicraft/servers"): error=2, No such file or directory
        at java.lang.ProcessBuilder.start(Unknown Source)
        at com.mcprohosting.com.nodecleanup.Preprocess.deleteCoreData(Preprocess.java:23)
        at com.mcprohosting.com.nodecleanup.handlers.CleanupHandler.executeCleanup(CleanupHandler.java:36)
        at com.mcprohosting.com.main.NodeCleaner.init(NodeCleaner.java:29)
        at com.mcprohosting.com.main.NodeCleaner.main(NodeCleaner.java:25)
Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(Unknown Source)
        at java.lang.ProcessImpl.start(Unknown Source)
        ... 5 more

However the command cd /home/root/multicraft/servers works perfectly fine.

I've also tried:

    ProcessBuilder probuilder = new ProcessBuilder("find . -name '*Backup*' -delete");
    probuilder.directory(new File("/home/root/multicraft/servers/"));
    probuilder.start();

Answer

Isaac picture Isaac · Feb 8, 2013

The ProcessBuilder constructor receives multiple strings - either as a List<String> or as a variable number of arguments, each of type String:

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html#ProcessBuilder(java.util.List) http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html#ProcessBuilder(java.lang.String...)

You're experiencing difficulties because you passed the entire command as one string, causing the JVM to look for a program called "find /home/root/multicraft/servers/ -name '*.log' -delete", not find.

You'll have to separate the long command into parts. Such as:

ProcessBuilder pb = new ProcessBuilder("find", ".", "-name", "'*CoreData*"...);