JAVA中的CMD命令集合

来源:互联网 发布:mac os 同版本更新 编辑:程序博客网 时间:2024/05/18 02:38

import
 java.io.BufferedReader;
import java.io.InputStreamReader;

 /**
  * 根据taskName杀死占用待删除文件的进程
  * @param taskName
  * @return
  */
 public boolean killTaskAboute(String taskName) {
    String cmd = Taskkill /IM "+ taskName + ".exe"
    executeCommend(cmd,"");
 }

 /**
  * 根据taskName开启进程
  * @param taskName
  * @return
  */
 public boolean startTaskAboute(String taskName){
    String srvPath  = "~\dcserver\bin"; // .exe的根目录(绝对路径),以IGS for JAVA开启dcs.exe为例
    taskName = taskName = ".EXE"; // taskName为.exe的应用程序
    executeCommend("cmd /c start \"" + taskName
         + "\" " + "\"" + srvPath + "\\" + taskName
         + "\"", srvPath);

    return true;
 }

 /**
  * 根据taskName重启进程
  * @param taskName
  * @return
  */
 public boolean startTaskAboute(String taskName){
    String srvPath  = "~\dcserver\bin"; // .exe的根目录(绝对路径),以IGS for JAVA开启dcs.exe为例
    taskName = taskName = ".EXE"; // taskName为.exe的应用程序

     //关闭
     executeCommend("taskkill /f /im " + srvName, null);
    //开启
    executeCommend("cmd /c start \"" + srvName + "\" " 
         + "\"" + srvPath + "\\" + srvName + "\"",
         null);

    return true;
 }

说明:
CMD下杀死进程有两种方法:
第一种就是4楼的方法:TaskKill  /IM (进程名字)
Runtime.getRuntime().exec("TaskKill /IM Notepad.exe"); 
第二种就是  ntsd -c q -pn (进程名字)
Runtime.getRuntime().exec("ntsd -c q -pn Notepad.exe"); 

一般第二种方法比第一种强劲一些
//----------------------------------------------------------------------------------------

 /**
  * 执行CMD命令(Windows|Linux系统)
  *
  * @param cmd
  * 命令
  * @param fPath
  * 命令文件所在路径
  * @return
  */
 public String executeCommend(String cmd, String fPath) {
    String rlt = "";
    InputStream in = null;
    try {
     Runtime run = Runtime.getRuntime();
     if (fPath == null) {
        in = run.exec(cmd).getInputStream();
     } else {
        File env = new File(fPath);
        if (env.exists()) {
        in = run.exec(cmd, null, env).getInputStream();
    }
}
String temp = "";
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
in, "gb2312"));
while ((temp = reader.readLine()) != null) {
buffer.append(temp);
}
rlt = buffer.toString();
reader.close();
} catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return rlt;
 }

 /**
  * Windows下,根据serviceName 开启/停止 Service,
  * @param serviceName 服务名称
  * @param type 操作类型 stop/start
  */
 public Message operWindowService(String type, String serviceName) {
    boolean msg = false;
    if (type.trim().isEmpty()) {
        type = "stop";
    }
    String cmd = "sc query " + serviceName;// cmd命令:询问一个服务的状态,也可以列举服务的状态类型
    String rlt = executeCommend(cmd, null);
    boolean installed = rlt.contains(serviceName) ? true : false;
    if (installed) {
        boolean stopped = rlt.contains("STOPPED") ? true : false;// 服务是否为停止状态
        if (type.equals("stop") && !stopped) {
            cmd = "net stop " + serviceName;//cmd命令:停止该服务 
            rlt = executeCommend(cmd, null);
            msg = rlt.contains("STOPPED") ? true : false;
        } else if (stopped) {
            cmd = "net start " + serviceName//cmd命令:启动该服务
            rlt = executeCommend(cmd, null);
            msg = rlt.contains("RUNNING") ? true : false; // 服务是否为开启状态
        } else {
            System.out.println("未安装该服务");
        }
    }
    return msg;
  }

//----------------------------------------------------------------------------------------------------------







0 0