Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples Java
 

Code Executing Commands


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Beginner

Published: 25th Apr 2009   Read: 1836 times  

Filed in: Java
Add Comment


 


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 Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





Popular Coders

# Coder NameHits
1. iamdvr35753
2. venki_madesh30854
3. srinivasaraobora22481
4. bora_srinivasarao11730
5. Kiran.jakkaraju4609
6. parmod kumar duhan4502
7. chowsys3971
8. Venkateswara Rao3713
9. Raju3391
10. Vamshidhar Matam2290

Active Coders

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape