Execute system commands in a Java Program

By: Rajan Printer Friendly Format    

Most often in your Java programs you will find a need to execute system DOS commands. You can execute any system commands that are OS specific and then read the output of the system command from your Java program for further processing within the Java program.

This sample Java Program executes the 'dir' command reads the output of the dir command prints the results. This is just for understanding the concept, however, you may execute just about any command using this Runtime.getRuntime().exec() command.

import java.io.*; 

public class doscmd 
public static void main(String args[]) 
Process p=Runtime.getRuntime().exec("cmd /c dir"); 
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
String line=reader.readLine(); 

catch(IOException e1) {} 
catch(InterruptedException e2) {} 


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Hey..can anyone please tell me how to run linux co
View Tutorial          By: ahmed at 2008-09-05 10:22:52

2. hey,m i suspend the process before start to execut
View Tutorial          By: koustubh nakate at 2010-04-01 22:16:16

3. Can u please tell me how to compile and execute a
View Tutorial          By: RJ at 2010-04-20 01:30:10

4. hey!!! can u pls tell me how to execute a java pro
View Tutorial          By: timothy at 2010-11-29 18:28:08

5. hey can u tell me a simple program to use the cmd
View Tutorial          By: Rakesh at 2011-08-08 10:56:47

6. hi gud mrng........
i wanna connectivity of

View Tutorial          By: sac at 2011-09-02 04:56:43

7. Hi,
I want to run a bat file, below are the

View Tutorial          By: Rajshekar at 2011-10-28 06:07:06

8. Thanks.I was searching the internet for a lot of t
View Tutorial          By: Habi at 2011-12-22 07:43:51

9. Hi guys.i have a simple doubt.
i want to ru

View Tutorial          By: Muthukumar at 2011-12-29 07:30:56

10. oh tnx a lot!i'v been lookin for a code like this!
View Tutorial          By: LES at 2012-01-13 12:55:09

11. Hey your help is really appreciated but I dont und
View Tutorial          By: farid khan at 2012-03-21 15:59:53

12. i want to make java editore in my major project.i
View Tutorial          By: pawan patel at 2012-04-14 11:15:19

13. I want to connect to your box and sniff your mapck
View Tutorial          By: lamer at 2012-06-19 22:29:51

14. Hi,
i am working on ocap xlet project. here

View Tutorial          By: swati at 2012-07-09 08:39:13

15. its not working.
View Tutorial          By: raja at 2012-08-29 11:44:45

16. Hey guys it's working. If not then remove p.waitFo
View Tutorial          By: dileep at 2013-01-04 19:51:38

17. FILE:- lpton.pl......

View Tutorial          By: Prats at 2013-02-02 17:33:40

18. Your article is very useful.

But I

View Tutorial          By: Me at 2013-04-07 10:20:00

19. Thank you very much sir.
One can simply exe

View Tutorial          By: rudhin at 2013-05-10 12:32:57

20. hai sir... this is ROSHAN I am searching for voice
View Tutorial          By: roshan at 2013-09-25 08:12:55

21. Runtime rt=Runtime.getRuntime();
String cmd

View Tutorial          By: saranya at 2014-10-30 05:36:36

22. Hi,
I want to write an Android Application,

View Tutorial          By: Kara at 2014-11-20 07:39:03

23. hi i want to run a c program in java program but i
View Tutorial          By: jyothi at 2014-11-27 06:40:27

24. Thank you. but Program can't run.
View Tutorial          By: channgam at 2015-03-06 06:58:01

25. Hi , I want to a read file having a list of telnet
View Tutorial          By: Greta Pereira at 2017-03-14 10:44:53