Java通过Shell执行Sqoop命令没日志的问题

修改执行部分的代码,改成用InputStream.read(byte[])的方法从流中读取数据

package com.example.demo.utils;

import java.io.*;

public class CMDExecute {

    public synchronized String run(String cmd) throws IOException {
        String line = null;
        String result = "";
        try {
            String[] commands={"/bin/sh", "-c",cmd};
            ProcessBuilder builder = new ProcessBuilder(commands); 
            builder.redirectErrorStream(true);
            Process process = builder.start();
            InputStream in = process.getInputStream();
            byte[] re = new byte[1024];
            while (in.read(re) != -1) {
                System.out.println(new String(re));
                result = result + new String(re);
            }
            in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}
posted @ 2018-09-21 10:58  周景白炎  阅读(1329)  评论(0编辑  收藏  举报