java代码调用shell脚本

public void importDateTohive() {
        try {
            String shpath = "/data/hadoop/percisettask/2_merge_userlog.sh";
            Process ps = Runtime.getRuntime().exec(shpath);
            ps.waitFor();

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            String result = sb.toString();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("数据刷新成功");
    }

上述代码中,下面这段就是调用shell执行的代码。

  String shpath = "/data/hadoop/percisettask/2_merge_userlog.sh";
  Process ps = Runtime.getRuntime().exec(shpath);
  ps.waitFor();

注意:shpath最好写绝对路径。

你可能感兴趣的:(java代码调用shell脚本)