java 调用 cmd 中的 tasklist 来获取特定进程的PID

//显示进程
  Process process = Runtime.getRuntime().exec("tasklist");
    
  Scanner in=new Scanner(process.getInputStream());
  while(in.hasNextLine()){
   String p=in.nextLine();
  //打印所有进程
  System.out.println(p);
   if(p.contains("javaw.exe")){
    StringBuffer buf=new StringBuffer();
    for(int i=0;i<p.length();i++){
     char ch=p.charAt(i);
     if(ch != ' '){
      buf.append(ch);
     }
    }
    //打印 javaw.exe的pid
  System.out.println(buf.toString().split("Console")[0].substring("javaw.exe".length()));
   }
  }
  
  //杀死进程,1,纯dos下,开cmd窗口 ntsd -c q -p PID
//  Runtime.getRuntime().exec("ntsd -c q -p 1528");
  //2 ,tskill PID(process ID)
//  Runtime.getRuntime().exec("tskill 3188");

原文地址:https://www.cnblogs.com/waruzhi/p/2434786.html