当文件确实存在时,ProcessBuilder给出“找不到文件”异常

正在运行将在Linux Web服务器上运行的应用程序中,以从某个目录中删除日志,但是我一直收到FileNotFound异常。

这是代码:

public static void deleteLOG() {

try {

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

probuilder.start();

} catch (IOException e) {

e.printStackTrace();

}

}

而且例外

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

但是,命令cd / home / root / multicraft / servers可以很好地工作。

我也尝试过:

    ProcessBuilder probuilder = new ProcessBuilder("find . -name '*Backup*' -delete");

probuilder.directory(new File("/home/root/multicraft/servers/"));

probuilder.start();

回答:

所述ProcessBuilder构造函数接收多个串-无论是作为一个List<String>或作为参数,每一种类型的可变数量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

…)

您遇到了麻烦,因为您将整个命令作为一个字符串传递了,导致JVM查找名为“ find /home/root/multicraft/servers/

-name '*.log' -delete”而不是的程序find

您必须将长命令分成多个部分。如:

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

以上是 当文件确实存在时,ProcessBuilder给出“找不到文件”异常 的全部内容, 来源链接: utcz.com/qa/426568.html

回到顶部