commons-exec CommandLine不接受“*”?

时间:2013-11-03 09:03:42

标签: java

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

我认为这是关于引用,但不知道如何解决它。

1 个答案:

答案 0 :(得分:1)

一种解决方法是将命令行放在.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