java 执行bat脚本,并监控执行结果

亲测可用 直接上工具类的代码:

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NoteUtil {
          
   

    /**
     * 执行bat脚本文件,弹出cmd黑窗
     * @param command 执行脚本文件命令
     * @param navigatePath 脚本文件所在文件夹地址
     */
    public static void runExecution(List<String> command, File navigatePath) {
          
   

        System.out.println(command);

        ProcessBuilder executeProcess=new ProcessBuilder(command);
        executeProcess.directory(navigatePath);
        try {
          
   
            Process resultExecution = executeProcess.start();
        }catch (Exception e){
          
   

        }
    }

    /**
     * 执行bat脚本,读取cmd输出内容
     * 注意:按行执行的,所以需要注意脚本编写的时候一条命令需要写进一行中,而且还要注意执行命令时所在的文件夹地址,如果是需要在特定文件夹下执行的命令,则需要每行命令开始时跳转到该目录
     * @param batPath 执行的脚本文件绝对路径
     */
    public static Map<String, String> runExecution2(String batPath) {
          
   
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        try {
          
   
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(batPath), "UTF-8"));//读取脚本
            String line;
            List<String> batList = new ArrayList();
            while ((line = br.readLine()) != null) {
          
   
                if (!line.equals("")) {
          
   
                    batList.add(line);
                }
            }
            for (String command : batList) {
          
   
                Process pro = Runtime.getRuntime().exec("cmd /c " + command);
                BufferedReader br1 = new BufferedReader(new InputStreamReader(pro.getInputStream(), "GBK"));
                BufferedReader br2 = new BufferedReader(new InputStreamReader(pro.getErrorStream(), "GBK"));
                System.out.println("Input Stream:");
                while ((line = br1.readLine()) != null) {
          
   
                    System.out.println(line);
                    sb1.append(line).append(System.getProperty("line.separator"));
                }
                System.out.println("Error Stream:");
                while ((line = br2.readLine()) != null) {
          
   
                    System.out.println(line);
                    sb2.append(line).append(System.getProperty("line.separator"));
                }
            }
        } catch (IOException e) {
          
   
            e.printStackTrace();
        } finally {
          
   
            Map map = new HashMap();
            map.put("getInputStreamString", sb1.toString());
            map.put("getErrorStreamString", sb2.toString());
            return map;
        }
    }


    public static void main(String[] args) {
          
   
        //cmd弹窗
        File jsFile=new File("D:\application\bat文件所在的目录");

        String cmdPrompt="cmd";
        String type="/c";
        String start = "start";
        String bat="run.bat";


        List<String> updateCommand=new ArrayList<String>();
        updateCommand.add(cmdPrompt);
        updateCommand.add(type);
        updateCommand.add(start);
        updateCommand.add(bat);

        runExecution(updateCommand,jsFile);

        //不需要弹窗,获得命令执行后的输入流,打印执行结果
        System.out.println(runExecution2("D:\application\脚本文件绝对路径.bat"));
    }

}
在测试的发现,如果弹窗来执行bat脚本,那么java代码中获取到的输入流中是空的 bat脚本使用 && 对两条语句进行拼接,语句拼接的时候注意注释,以免脚本都拼接到注释后面去了导致执行不到
经验分享 程序员 微信小程序 职场和发展