execute Bat Cmd - Java Native OS

Java examples for Native OS:Shell Command

Description

execute Bat Cmd

Demo Code


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    public static void main(String[] argv) throws Exception{
        String cmd = "java2s.com";
        exeuteBatCmd(cmd);/*from w w  w. ja  v a2  s  . c  o  m*/
    }
    
    public static void exeuteBatCmd(String... cmd) {
        try {
            String path = createBatFile(cmd);
            Thread.sleep(3000);
            exeuteCmdFile(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String createBatFile(String... cmd) {
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) {
            String path = System.getProperty("user.dir") + File.separator
                    + "temp" + DateUtil.getCurrentDateTimeAsId() + ".bat";
            FileUtil.createFile(path);
            StringBuffer content = new StringBuffer();
            for (int i = 0; i < cmd.length; i++) {
                content.append(cmd[i] + "\r\n");
            }
            FileUtil.writeFile(path, content.toString());
            return "\"" + path + "\"";
        } else {
            String path = System.getProperty("user.dir") + File.separator
                    + "temp.sh";
            FileUtil.createFile(path);
            StringBuffer content = new StringBuffer();
            content.append("#!/bin/sh").append("\n");
            for (int i = 0; i < cmd.length; i++) {
                content.append(cmd[i] + "\r\n");
            }
            FileUtil.writeFile(path, content.toString());
            return path;
        }
    }
    
    public static void exeuteCmdFile(String filePath) {
        try {
            Runtime run = Runtime.getRuntime();
            Process pro = run.exec(filePath);
            // ??file
            FileUtil.deleteFile(filePath.replace("\"", ""));

            BufferedReader br = null;
            StringBuffer sb = new StringBuffer();
            InputStreamReader isr = new InputStreamReader(
                    pro.getInputStream(), "utf-8");
            br = new BufferedReader(isr);
            String line = br.readLine();
            while (line != null) {
                sb.append(line + "\r\n");
                line = br.readLine();
                System.err.println(line);
            }

            try {
                pro.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();
                        br = null;
                    }
                    if (isr != null) {
                        isr.close();
                        isr = null;
                    }
                    if (pro != null) {
                        pro.destroy();
                        pro = null;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials