PID through Java

 

Through command prompt we can get list of process and corresponding PID's by using tasklist command. In this tutorial lets see how to get list of PID's of particular process like Notepad, PowerPoint, MS-Word etc., Here in this example lets see list of Notepad process and corresponding Process Ids (PID).
PID through java



public class PID {

 public static void main(String[] args)  {
  
  try {
   Runtime runtime = Runtime.getRuntime();
         Process process = runtime.exec("tasklist /fi \"imagename eq NOTEPAD*\" /NH");
         process.getOutputStream().close();
         InputStream inputStream = process.getInputStream();
         InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
         BufferedReader bufferedrReader = new BufferedReader(inputstreamreader);
            
   String strLine = "";
         boolean firstLine = true;
        
   while ((strLine = bufferedrReader.readLine()) != null) {
    
    if(firstLine){
     firstLine = false;
     continue;
    }
    StringTokenizer tok = new StringTokenizer(strLine);
    
    while (tok.hasMoreElements()) {
     String object = (String) tok.nextElement();
     String object1 = (String) tok.nextElement();
     System.out.print(object+" : "+ object1);
     break;
    }
    System.out.println();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}


OUTPUT:


notepad.exe : 840
notepad.exe : 1884



No comments:
Write comments