我尝试了CommandLine#parse和addArgument:

DefaultExecutor executor = new DefaultExecutor();

executor.execute(CommandLine.parse("ls /Users/jizhang/*.py"));

CommandLine cmd = new CommandLine("ls");
cmd.addArgument("/Users/jizhang/*.py", false);
executor.execute(cmd);


例外是:

ls: /Users/jizhang/*.py: No such file or directory
Exception in thread "main" org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)


但实际上该文件夹中有一些文件:

prey:~ jizhang$ ls /Users/jizhang/*.py
/Users/jizhang/ana.py        /Users/jizhang/send_image.py
/Users/jizhang/push.py       /Users/jizhang/t.py


我相信这是关于报价的,但不知道如何解决。

最佳答案

一种解决方法是将命令行放入.sh文件,然后使用sh命令运行它:

executor.execute(CommandLine.parse("/bin/sh /home/path_to_my_cmd.sh"));

// content of file path_to_my_cmd.sh
ls /Users/jizhang/*.py

08-27 12:52