java代码获取进程是否存在、编写bat脚本判断进程,不存在则启动

青旅半醒 2022-02-24 02:38 634阅读 0赞

java环境jdk的bin目录下copy文件javaw.exe为affectivew.exe

脚本如下:

@echo off
tasklist | find /i “affectivew.exe” && echo [ %time:~,-3% ]存在进程affectivew.exe|| start %JAVA_HOME%\bin\affectivew.exe -jar C:\Users\Administrator\Desktop\testProess\nb-eureka.jar monitorMonkeyLog &
exit

public static void main(String[] args) throws InterruptedException {
boolean status = getTaskStatus();
System.out.println(“进程运行状态:”+status);
}

  1. public static boolean getTaskStatus()\{
  2. /\*
  3. \* 下面先是获取进程列表
  4. \*/
  5. Runtime runtime = Runtime.getRuntime();
  6. List tasklist = new ArrayList();
  7. try \{
  8. Process process = runtime.exec("tasklist");
  9. BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
  10. String s = "";
  11. while ((s = br.readLine()) != null) \{
  12. if ("".equals(s)) \{
  13. continue;
  14. \}
  15. tasklist.add(s+" ");
  16. \}
  17. // 获取每列最长的长度
  18. String maxRow = tasklist.get(1) + "";
  19. String\[\] maxCol = maxRow.split(" ");
  20. // 定义映像名称数组
  21. String\[\] taskName = new String\[tasklist.size()\];
  22. for (int i = 0; i < tasklist.size(); i++) \{
  23. String data = tasklist.get(i) + "";
  24. for (int j = 0; j < maxCol.length; j++) \{
  25. switch (j) \{
  26. case 0:
  27. taskName\[i\]=data.substring(0, maxCol\[j\].length()+1).trim();
  28. data=data.substring(maxCol\[j\].length()+1);
  29. break;
  30. \}
  31. \}
  32. \}
  33. List<String> taskNameList = Arrays.asList(taskName);
  34. boolean taskFlag = taskNameList.contains("affectivew.exe");
  35. return taskFlag;
  36. \} catch (Exception e) \{
  37. e.printStackTrace();
  38. \}
  39. return false;

}

发表评论

表情:
评论列表 (有 0 条评论,634人围观)

还没有评论,来说两句吧...

相关阅读