CheckJarPid.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. package com.youlai.boot;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. public class CheckJarPid {
  5. public static void main(String[] args) {
  6. String jarName = "jeecg-cloud-nacos-3.2.0.jar";
  7. String command = "cmd /c tasklist /FI \"IMAGENAME eq java.exe\" /FO CSV /NH | findstr \"" + jarName.replace(".", "\\.") + "\"";
  8. try {
  9. Process process = Runtime.getRuntime().exec(command);
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  11. String line;
  12. // 读取命令输出
  13. while ((line = reader.readLine()) != null) {
  14. // 解析CSV格式的输出,第一列是PID
  15. String[] parts = line.split(",");
  16. if (parts.length > 0) {
  17. String pid = parts[0].trim();
  18. // 输出PID
  19. System.out.println("PID: " + pid);
  20. // 由于我们可能只关心第一个匹配项,所以找到后可以退出循环
  21. break;
  22. }
  23. }
  24. reader.close();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }