zoukankan      html  css  js  c++  java
  • java 执行bat批处理文件 并关闭cmd窗口

    java 执行bat批处理文件 并关闭cmd窗口

    import java.io.IOException;
    
    public class CmdMain {
     public static void main(String[] args){
      
      //执行批处理文件
      String strcmd="cmd /c start  D:\antrelease.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 block
       e.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();
      }
     }
    }
    
  • 相关阅读:
    程序人生,编程思想
    CentOS Linux Jenkins安装、部署、更新
    Git常用命令
    U盘安装Mac OS X要点
    Shell执行*.sql
    WebStorm远程调试Node.js
    svn常用命令
    敏捷开发相关编辑思想(SOA、DDD、REST、CQRS)
    VisualVM远程监控Java
    centos搭建git服务
  • 原文地址:https://www.cnblogs.com/nidongde/p/5396586.html
Copyright © 2011-2022 走看看