我正在尝试创建一个调用“ls * .xml”的进程。
此代码可以正常使用:
Process p=Runtime.getRuntime().exec("ls build.xml");
build.xml
此代码错误:
Process p=Runtime.getRuntime().exec("ls *.xml");
// this is the output
Cannot run program "ls *.xml": error=2, No such file or directory
有什么想法吗?
答案 0 :(得分:1)
这是因为通配符扩展是由Bash shell完成的,而不是由ls
命令完成的。
如果你想要的只是列表文件,请使用Files.newDirectoryStream:
Files.newDirectoryStream(Paths.get(".")), p -> p.toString().endsWith(".xml")).forEach(System.out::println)