Java getRuntime.exec()不工作,ubuntu 13.04
当我从终端执行以下命令时,它工作正常:Java getRuntime.exec()不工作,ubuntu 13.04,java,process,runtime.exec,Java,Process,Runtime.exec,当我从终端执行以下命令时,它工作正常: rm -f /home/folkman/Desktop/DimacsProba/* 但是,我想用Java执行它。我使用的是Netbeans,当我使用较旧版本的Ubuntu时,它起到了作用: Runtime.getRuntime().exec("rm -f /home/folkman/Desktop/DimacsProba/*"); 现在,使用Ubuntu 13.04。以下任何一项都不起作用: //1 Runtime.getRuntim
rm -f /home/folkman/Desktop/DimacsProba/*
但是,我想用Java执行它。我使用的是Netbeans,当我使用较旧版本的Ubuntu时,它起到了作用:
Runtime.getRuntime().exec("rm -f /home/folkman/Desktop/DimacsProba/*");
现在,使用Ubuntu 13.04。以下任何一项都不起作用:
//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();
也许这不是因为Ubuntu,但我真的不知道。请帮忙!
谢谢大家! 通配符是一种shell功能,因此如果您使用exec,它们将按字面意思进行处理。如果您有一个名为
*
的文件,它将被删除,但我怀疑您需要一个shell来扩展*
试一试
为什么不通过JavaAPI删除文件呢?调用shell来删除文件没有什么错,但是这样会牺牲可移植性。为了更好的可移植性,如果它适合您的用例,您可能希望使用Java文件API递归删除dir.Read中的文件(并实现)中的所有建议。这可能会解决问题。如果没有,它应该提供更多关于失败原因的信息。然后忽略它引用的
exec
,并使用ProcessBuilder
构建流程。还可以将字符串arg
分解为字符串[]args
,以说明本身包含空格的参数(如在s.2和s.3中)。@user2581772您可以通过单击左侧的勾号接受答案;)
"sh", "-c", "rm -f /home/folkman/Desktop/DimacsProba/*"