ProcessBuilder无法识别嵌入式命令

时间:2019-06-26 13:50:16

标签: java terminal processbuilder

我有方法

final ProcessBuilder processBuilder = new ProcessBuilder(this.cmd);
        processBuilder.redirectErrorStream(true);
        try {
            final Process process = processBuilder.start();
            try (final BufferedReader reader =
                         new BufferedReader(
                                 new InputStreamReader(
                                         process.getInputStream()
                                 )
                         )
            ) {
                while (reader.ready()) {
                    System.out.println(reader.readLine());
                }
                process.waitFor(this.timeOut, TimeUnit.SECONDS);
            }
        } catch (final Exception exc) {
            throw new IllegalStateException("Terminal command execution exception", exc);
        }

此方法从Constructor中获取cmd(字符串数组),然后执行它。这是cmd的值

new String[]{"cat","$(cat /home/Downloads/tmp/aqnYtUSVFp.txt)",">","/home/Downloads/hello.ts"}

所以整个命令看起来像这样cat $(cat home/Downloads/tmp/aqnYtUSVFp.txt) > /home/Downloads/hello.ts 如果我在终端中运行它,它可以正常工作,但是ProcessBuilder向我显示以下错误

cat: '$(cat /home/Downloads/aqnYtUSVFp.txt)': No such file or directory
cat: '>': No such file or directory
cat: /home/Downloads/hello.ts: No such file or directory

我想念什么。为什么ProccBuilder无法识别此命令?

0 个答案:

没有答案