Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Code Executing Commands


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Beginner

Published: 25th Apr 2009   Read: 5390 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This code shows how to execute linux/DOS command using java

 


Sample Code
  1.  
  2.  
  3. import java.io.*;
  4.  
  5. public class Main {
  6.    
  7. public static void main(String[] args) {
  8.         try{
  9. String s = null;
  10. try{
  11. Process p = Runtime.getRuntime().exec("command");
  12. BufferedReader stdInput = new BufferedReader(new
  13. InputStreamReader(p.getInputStream()));
  14. BufferedReader stdError = new BufferedReader(new
  15. InputStreamReader(p.getErrorStream()));
  16. System.out.println("Here is the standard output of the command:n");
  17. while ((s = stdInput.readLine()) != null)
  18. {
  19. System.out.println(s);
  20. }
  21. }
  22. catch(Exception e) {}
  23.         Runtime rt=Runtime.getRuntime();
  24.         rt.exec("ls");
  25.         // TODO code application logic here
  26.     }
  27.         catch(Exception e){
  28.         }
  29.         }
  30.     }
  31.    
  32.  
Copyright GeekInterview.com


Next Article: Jasper Report Application


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora130613
2. iamdvr97642
3. venki_madesh35132
4. Raju18459
5. parmod kumar duhan15744
6. Kiran.jakkaraju14037
7. chowsys7450
8. Venkateswara Rao6469
9. ashish.cns6342
10. Vamshidhar Matam5424

Active Coders

Refined Tags