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;
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM