Tuesday, 27 May 2014

Run System Command in Java

import java.io.*;


public class RunSystemCmd {
 public static void runCmd(String cmd) {
  String s;
  String 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);    
 }
    
 public static void 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);
 }
    
 public static void runMyCmd(String s){
  System.out.println(s +"\n");
  String osType =  getOsDetails();
  
  if(osType.contains("windows") || osType.contains("Windows") || osType.contains("WINDOWS")){
   runWindowsCmd(s);
  }
  else{
   runCmd(s);
  }
 }
    
 static public String getOsDetails(){
  return System.getProperty("os.name");
 }
 
 public static void main(String args[]){
  String cmd = args[0];
  runMyCmd(cmd);
 }
}

Run the program like below
      java RunSystemCmd tasklist

Output
tasklist


Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0         24 K
System                           4 Services                   0      3,436 K
smss.exe                       412 Services                   0      1,156 K
csrss.exe                      548 Services                   0      5,708 K
csrss.exe                      648 Console                    1     61,508 K
wininit.exe                    656 Services                   0      5,464 K
winlogon.exe                   712 Console                    1      8,156 K
services.exe                   764 Services                   0     12,304 K
lsass.exe                      776 Services                   0     11,992 K
lsm.exe                        784 Services                   0      4,732 K
                                                             Home

No comments:

Post a Comment