Execute system commands in a Java Program

By: Rajan  

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) {} 


Archived Comments

1. Hi , I want to a read file having a list of telnet commands and execute them one by one in a command
View Tutorial          By: Greta Pereira at 2017-03-14 10:44:53

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

3. hi i want to run a c program in java program but is is saying n valid directory.my c program is in
View Tutorial          By: jyothi at 2014-11-27 06:40:27

4. Hi,
I want to write an Android Application, which will create a log file of all actions relat

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

5. Runtime rt=Runtime.getRuntime();
String cmd[]={"cmd.exe", "/C", "c:\

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

6. hai sir... this is ROSHAN I am searching for voice input and output operation on computer so if u ha
View Tutorial          By: roshan at 2013-09-25 08:12:55

7. Thank you very much sir.
One can simply execute linux commands too this way
just need

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

8. Your article is very useful.

But I fear you have some cretins for an audience, judgi

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

9. FILE:- lpton.pl......

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

10. Hey guys it's working. If not then remove p.waitFor(); and second catch(InterruptedException e2) {
View Tutorial          By: dileep at 2013-01-04 19:51:38

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

12. Hi,
i am working on ocap xlet project. here, i want to call initXlet() of ocap through plugin

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

13. I want to connect to your box and sniff your mapckets. please tech me how to do this like all these
View Tutorial          By: lamer at 2012-06-19 22:29:51

14. i want to make java editore in my major project.i face problem in compile and run option.so plz help
View Tutorial          By: pawan patel at 2012-04-14 11:15:19

15. Hey your help is really appreciated but I dont understand why they use "cmd /c dir" as arg
View Tutorial          By: farid khan at 2012-03-21 15:59:53

16. oh tnx a lot!i'v been lookin for a code like this!this really helps me.so can i modify it so that it
View Tutorial          By: LES at 2012-01-13 12:55:09

17. Hi guys.i have a simple doubt.
i want to run the appliction using system program in java . sa

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

18. Thanks.I was searching the internet for a lot of time to find out how to run an external file throug
View Tutorial          By: Habi at 2011-12-22 07:43:51

19. Hi,
I want to run a bat file, below are the steps to do it manually
1.Open comand prom

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

20. hi gud mrng........
i wanna connectivity of my project with system......
can anyone te

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

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

22. hey!!! can u pls tell me how to execute a java program???
pls!!! it's needed!!!

View Tutorial          By: timothy at 2010-11-29 18:28:08

23. Can u please tell me how to compile and execute a c program through a java Program....
View Tutorial          By: RJ at 2010-04-20 01:30:10

24. hey,m i suspend the process before start to execute in windows,tell me procedure or system calls.
View Tutorial          By: koustubh nakate at 2010-04-01 22:16:16

25. Hey..can anyone please tell me how to run linux commands and telnet to a port using java..any help w
View Tutorial          By: ahmed at 2008-09-05 10:22:52

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial