1

我尝试了 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

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

4

1 回答 1

2

一种解决方法是将命令行放入一个 .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
于 2013-11-03T11:16:43.163 回答