Loading

eclipse 调用cmd运行DataX

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TestDatax {
    Process process;
    public void CmdTest(String json) throws IOException {
        process=Runtime.getRuntime().exec("cmd");   //获取控制台
        new Thread(){
            public void run() {
                try {
                    BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));  //获取输入流
                    bw.write("CHCP 65001");
                    bw.newLine();

                    bw.write("d:");
                    bw.newLine();

                    bw.write("cd D:\\datax\\bin");
                    bw.newLine();//因为读取时是一行行读取的,不加newline无法继续往下读

                    bw.write("python datax.py "+json);
                    bw.newLine();


                    bw.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();;
        new Thread() {
            public void run() {
                try {
                    BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String cmdout="";
                    while ((cmdout=br.readLine())!=null) {
                        System.out.println(cmdout);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();

    }

    public static void main(String[] args) throws IOException {
        TestDatax cmd=new TestDatax();
        String json="D:\\datax\\job\\job.json";
        cmd.CmdTest(json);

    }
}

from:https://www.cnblogs.com/goubb/p/12416625.html

控制台出现乱码,因为已经在cmd输入了CHCP 65001,所以只需要更改eclipse的编码方式为utf-8

posted @ 2020-03-05 17:24  李旭2018  阅读(263)  评论(0编辑  收藏  举报