Monday 21 April 2014

Find processes in Unix in Java


import java.io.*;
class OpenSystemProcess{

 static String runCmd(String cmd1){
  String s;
  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 runCmd " + E);
  }
  
  System.out.println(output);
  return output;
 }
 
 public static void main(String args[]){
  System.out.println(runCmd("ps -eaf"));
 }
}

Output





 
                                                             Home

No comments:

Post a Comment