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