ProcessBuilder不使用通配符执行命令

时间:2017-08-01 09:52:39

标签: java unix wildcard processbuilder

我正在尝试编写一个实用程序java程序,它在unix框中调用perl脚本并显示脚本的输出。问题是当我执行command2

String[] command2 = {"/archive/scripts/grep.pl", "/apps/ws/logs/api.log"};

输出正确。但是当我使用command1时,

String[] command1 = {"/archive/scripts/grep.pl", "/apps/ws/logs/*"};

我得到以下例外:

Can't open /apps/ws/logs/*: No such file or directory at /archive/scripts/grep.pl line 160.

以下是供您参考的完整代码:

    StringBuffer output = new StringBuffer();
    try {
        ProcessBuilder processBuilder = new ProcessBuilder(command1);
        LOGGER.debug("Command: "+processBuilder.command());
        Process process = processBuilder.start();
        process.waitFor();
        BufferedReader br;
        if (process.exitValue() == 0) {
            LOGGER.debug("Inside if block. Exit value: "+process.exitValue());
            String line;
            br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = br.readLine()) != null) {
                output.append(line + "\n");
            }
        } else {
            LOGGER.debug("Inside Else block. Exit value: "+process.exitValue());
            br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                output.append(line + "\n");
            }
        }
    } catch (Exception e) {
        LOGGER.debug("Exception thrown"+e.getStackTrace());
        output.append(e.getStackTrace().toString());
        e.printStackTrace();
    }
    return output.toString();

我无法理解问题所在。有没有办法实现这一目标。

1 个答案:

答案 0 :(得分:3)

使用命令shell解释通配符

~/sub/