package com.youlai.boot;

import java.io.BufferedReader;
import java.io.InputStreamReader;  
  
public class CheckJarPid {  
    public static void main(String[] args) {  
        String jarName = "jeecg-cloud-nacos-3.2.0.jar";  
        String command = "cmd /c tasklist /FI \"IMAGENAME eq java.exe\" /FO CSV /NH | findstr \"" + jarName.replace(".", "\\.") + "\"";  
  
        try {  
            Process process = Runtime.getRuntime().exec(command);  
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
            String line;  
  
            // 读取命令输出  
            while ((line = reader.readLine()) != null) {  
                // 解析CSV格式的输出，第一列是PID  
                String[] parts = line.split(",");  
                if (parts.length > 0) {  
                    String pid = parts[0].trim();  
                    // 输出PID  
                    System.out.println("PID: " + pid);  
                    // 由于我们可能只关心第一个匹配项，所以找到后可以退出循环  
                    break;  
                }  
            }  
  
            reader.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}