• java实现Windows系统进程查询及kill进程和重启进程(适用查看邮件等)


     1 /**
     2      * @desc 启动进程
     3      * @author zp
     4      * @date 2018-3-29
     5      */
     6     public static void startProc(String processName) { 
     7          log.info("启动应用程序:" + processName);  
     8          if (StringUtils.isNotBlank(processName)) {  
     9              try {  
    10                  Desktop.getDesktop().open(new File(processName));  
    11              } catch (Exception e) {  
    12                  e.printStackTrace();  
    13                  log.error("应用程序:" + processName + "不存在!");  
    14              }  
    15          }   
    16     }
    17     /**
    18      * @desc 杀死进程
    19      * @author zp
    20      * @throws IOException 
    21      * @date 2018-3-29
    22      */
    23     public static void killProc(String processName) throws IOException {  
    24         log.info("关闭应用程序:" + processName);  
    25         if (StringUtils.isNotBlank(processName)) {  
    26             executeCmd("taskkill /F /IM " + processName);  
    27         } 
    28     }
    29     /**
    30      * @desc 执行cmd命令 
    31      * @author zp
    32      * @date 2018-3-29
    33      */
    34     public static String executeCmd(String command) throws IOException {  
    35         log.info("Execute command : " + command);  
    36         Runtime runtime = Runtime.getRuntime();  
    37         Process process = runtime.exec("cmd /c " + command);  
    38         BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));  
    39         String line = null;  
    40         StringBuilder build = new StringBuilder();  
    41         while ((line = br.readLine()) != null) {  
    42             log.info(line);  
    43             build.append(line);  
    44         }  
    45         return build.toString();  
    46     }  
    47     /**
    48      * @desc 判断进程是否开启
    49      * @author zp
    50      * @date 2018-3-29
    51      */
    52     public static boolean findProcess(String processName) {
    53         BufferedReader bufferedReader = null;
    54         try {
    55             Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName +'"');
    56             bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    57             String line = null;
    58             while ((line = bufferedReader.readLine()) != null) {
    59                 if (line.contains(processName)) {
    60                     return true;
    61                 }
    62             }
    63             return false;
    64         } catch (Exception ex) {
    65             ex.printStackTrace();
    66             return false;
    67         } finally {
    68             if (bufferedReader != null) {
    69                 try {
    70                     bufferedReader.close();
    71                 } catch (Exception ex) {}
    72             }
    73         }
    74     }
    75     
    76      //downfile = "D:\DownFile\DownFile.exe";
    77     String url = request.getParameter("downfile"); 
    78     String procName = url.substring(url.lastIndexOf("\")+1,url.length());
    79     boolean exist= findProcess(procName); 
    80     try {
    81         if (exist) { 
    82             // 存在,那么就先杀死该进程
    83             killProc(procName);
    84             // 在启动
    85             startProc(url);
    86         }else {
    87             startProc(url);
    88         }
    89     } catch (Exception e) {
    90         // TODO: handle exception
    91         log.error("重启/杀死提取程序失败。。。");  
    92     }
  • 相关阅读:
    OpenGL编程 基础篇(七)对象的变换——用实心体绘制3D场景
    mysql在查询结果列表前添加一列递增的序号列(最简)
    将中文字符串分割为数组 解决str_split中文乱码php
    file_get_contents 抓取网页乱码。
    js下的sleep实现
    js获取当前时间(昨天、今天、明天)
    JPA为字段设置默认值
    js实现点击按钮弹出上传文件的窗口
    Bootstrap使用模态框modal实现表单提交弹出框
    springboot使用Freemarker继承
  • 原文地址:https://www.cnblogs.com/xiaoyue1606bj/p/10977799.html
Copyright © 2020-2023  润新知