Java 调用batch

 

public class Batch {

    public static void main(String[] args) throws Exception {
        executeBatch();
    }

    private static void executeBatch() {
        BufferedReader in = null;
        Runtime r = Runtime.getRuntime();
        Process proc;
        String command = "./batch.bat";

        String line = null;

        try {
            proc = r.exec(command);
            in = new BufferedReader(new InputStreamReader(proc.getInputStream()));

            while ((line = in.readLine()) != null) {
                System.out.println("LINE:" + line);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 

// batch.bat
echo hahaha
echo sdfd

 

posted @ 2016-12-27 08:27  牧 天  阅读(521)  评论(0)    收藏  举报