当使用ProcessBuilder erorr:missing`-exec'参数时,在Linux中工作的Java find和sed命令不起作用

时间:2019-02-11 23:04:23

标签: java sed exec

我在linux中有一个简单的命令可以正常工作,看起来像这样:

find /home/my/test -type f -name my.file.properties -print -exec sed -i.bak 's/'2222'/'4444'/' {} \;

当我尝试使用ProcessBuilder即时执行命令时:

find: missing argument to `-exec'
Exception in thread "main" java.lang.AssertionError: runCommand returned 1

这是我的运行方式:

String sreachSedStr = "\'s/'2222'/'4444'/\'";
 String fileFullPath = "/home/my/test"
           runCommandEx(null,
                    "find",
                    fileFullPath,
                    "-type",
                    "f",
                    "-name",
                    "my.file.properties",
                    "-print",
                    "-exec",
                    "sed",
                    "-i.bak",
                    sreachSedStr,
                    "{}",
                    "\\;");





public static void runCommandEx(Path directory, String... command)  {
        if(directory != null) {
            if (!Files.exists(directory)) {
                throw new RuntimeException("can't run command in non-existing directory '" + directory + "'");
            }
        }
        try {
            //LOG
            String commandline="";
            for (String arg : command) {
                commandline+=arg+" ";
            }
            System.out.println("runCommand directory"+(directory==null?"none ":directory)+" "+commandline);
             ProcessBuilder pb = new ProcessBuilder();
            pb.command(command);
            if(directory!=null){
                pb.directory(directory.toFile());
            }

            Process p = pb.start();
            getProcessBuilder(p);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

private static void  getProcessBuilder(Process p) throws InterruptedException {
        StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream());
        StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream());
        outputGobbler.start();
        errorGobbler.start();
        int exit = p.waitFor();
        errorGobbler.join();
        outputGobbler.join();
        if (exit != 0) {
            throw new AssertionError(String.format("runCommand returned %d", exit));
        }
    }

 private static class StreamGobbler extends Thread {

        private final InputStream is;

        private StreamGobbler(InputStream is) {
            this.is = is;
        }

        @Override
        public void run() {
            try (BufferedReader br = new BufferedReader(new InputStreamReader(is));) {
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

}

这是怎么了? 看起来像

时引发的错误
int exit = p.waitFor();
if (exit != 0)

0 个答案:

没有答案