Monday, 21 April 2014

Find processes in Windows using Java


import java.io.*;
class WindowProcess{

 static String runWindowsCmd(String cmd){
  String s;
  String cmd1 = "cmd.exe /c ";
  cmd1 = cmd1 + cmd;
  String output = "";
       
  try{
   Process proc=Runtime.getRuntime().exec(cmd1);        
   BufferedReader stdInput;
   BufferedReader stdError;

   stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
   stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

   while ((s = stdInput.readLine()) != null){
    output = output + s + "\n";
   }        

   while ((s = stdError.readLine()) != null){
    output = output + s + "\n";  
   }                
  }
  catch(Exception E){
   System.out.println("Exception caught in RunWindowsCmd " + E);
  }
  
  System.out.println(output);
  return output;
 }
 
 public static void main(String args[]){
  System.out.println(runWindowsCmd("tasklist"));
 }
}

Output




                                                            Home

No comments:

Post a Comment