java執行linux命令

try {
            Process child = Runtime
                    .getRuntime()
                    .exec(
                            new String[] {
                                    "/bin/sh",
                                    "-c",
                                    "grep 'sth' xx.log|cut -d ' ' -f6,1|sort |uniq -c |sort" },
                            null,
                            new File("path"));

            BufferedReader in = new BufferedReader(new InputStreamReader(child
                    .getInputStream()));

            String c = null;
            while ((c = in.readLine()) != null) {
                System.out.println(c + "<br/>");
            }
            child.waitFor();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print(e.getMessage());
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章