本文主要是介绍Java执行bat批处理文件,并关闭cmd窗口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.baobaotao.test;import java.io.IOException;public class CmdMain {public static void main(String[] args) {// 执行批处理文件String strcmd = "cmd /c start E:\\run.bat";Runtime rt = Runtime.getRuntime();Process ps = null;try {ps = rt.exec(strcmd);} catch (IOException e1) {e1.printStackTrace();}try {ps.waitFor();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}int i = ps.exitValue();if (i == 0) {System.out.println("执行完成.");} else {System.out.println("执行失败.");}ps.destroy();ps = null;// 批处理执行完后,根据cmd.exe进程名称// kill掉cmd窗口new CmdMain().killProcess();}public void killProcess() {Runtime rt = Runtime.getRuntime();Process p = null;try {rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");} catch (IOException e) {e.printStackTrace();}}
}
这篇关于Java执行bat批处理文件,并关闭cmd窗口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!