Execute system commands in a Java Program

By: Rajan Emailed: 1675 times Printed: 2158 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

Hey..can anyone please tell me how to run linux commands and telnet to a port using java..any help would be highly appreciated.

Thank you in advance

View Tutorial          By: ahmed at 2008-09-05 10:22:52
2. View Comment

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
3. View Comment

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
4. View Comment

hi gud mrng........
i wanna connectivity of my project with system......
can anyone tell me how can i do it..........plz give me that coding.......

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

I want to run a bat file, below are the steps to do it manually
1.Open comand prompt
2. go to d:\batfiles folder
3. And run 'StreamCntroller.bat console' command.

Please help to achieve the same using java.

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

Thanks.I was searching the internet for a lot of time to find out how to run an external file through java. Thank you guys.

View Tutorial          By: Habi at 2011-12-22 07:43:51
7. View Comment

Hi guys.i have a simple doubt.
i want to run the appliction using system program in java . say how to do?
for example to run opera browser using system programming
mail to me
thanks peoples

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

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 can write,say,a certain output from it to a certain application that is stil running?or what i tryna say is,can mi java program interact with the applications themseves??
thx in advance for your help! :-)

View Tutorial          By: LES at 2012-01-13 12:55:09
9. View Comment

Hey your help is really appreciated but I dont understand why they use "cmd /c dir" as argument to exec() what does it mean?Please mail me Thank u...

View Tutorial          By: farid khan at 2012-03-21 15:59:53
10. View Comment

i want to make java editore in my major project.i face problem in compile and run option.so plz help me.

View Tutorial          By: pawan patel at 2012-04-14 11:15:19
11. View Comment

i am working on ocap xlet project. here, i want to call initXlet() of ocap through plugin (c++) code. I tried with JNI but it does not worked. an anyone geive me the way to do this.
Thanx in advanced!

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

its not working.

View Tutorial          By: raja at 2012-08-29 11:44:45
13. View Comment

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
14. View Comment

FILE:- lpton.pl......
# Parallel port CGI script
# Send HTTP headers
print "Content-type: text/html\n\n";
#print "Content-type: text/html\n\n";
#print "Content-type: text/html\n\n";
# Output web page data

print "Parallel port controlled";
print "<a href=\"/Test/index2.html\">Go back to controlling page</a>";

system('/usr/sbin/test 0xff');
This is a script for executing a c program(test) under /usr/sbin/ directory. This script works absolutely fine when this file lpton.pl is executed through command line. But when i try to execute the same file(lpton.pl) through a web server (lampp) only the print statement is getting displayed on browser but the c program is not getting executed....plzz do help me with it......

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

Your article is very useful.

But I fear you have some cretins for an audience, judging from the retarded comments.

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

Thank you very much sir.
One can simply execute linux commands too this way
just need to change :
"Process p=Runtime.getRuntime().exec("cmd /c dir"); "
"Process p=Runtime.getRuntime().exec("ls"); " // or something else
thanks once again.

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

hai sir... this is ROSHAN I am searching for voice input and output operation on computer so if u have any related information pls send on syedroshan.roshan@yahoo.com contect: +91 9642345291

View Tutorial          By: roshan at 2013-09-25 08:12:55
18. View Comment

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

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

I want to write an Android Application, which will create a log file of all actions related to writing and reading of memory.

Please help and explain how to write this application using java for Android!

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

hi i want to run a c program in java program but is is saying n valid directory.my c program is in c drive ,TDM-GCC-64 ,bin,hello.c. please sent the answer to my mail

View Tutorial          By: jyothi at 2014-11-27 06:40:27

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Rajan
WebWork 2 is now Apache Struts 2
Using JDBC to connect to MySQL from Java Program
Execute system commands in a Java Program
Cookies using JSP or Java Bean
Sample Java Script that displays a movable clock
Java Chat Client Sample Applet

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
Stack example in Java - push(), pop(), empty(), search()
FileReader and FileWriter example program in Java
Method Overriding in Java
Read from a COM port using Java program
The Basic Structure of a Simple Java program
XML and Java - Parsing XML using Java Tutorial
Using StringTokenizer in Java
How to use ArrayList in Java
Recursion in java
Student marks calculation program in Java
Method Overloading (function overloading) in Java
How to use Iterator in Java
Abstract classes in Java
append() in Java
Most Emailed Articles (in Java)
Tomcat and httpd configured in port 8080 and 80
Update contents of a file within a jar file
Vector in Java - Sample Program
Get user input in Java
FilenameFilter - sample program in Java
How to Send SMS using Java Program (full code sample included)
Count number of vowels, consonants and digits in a String in Java
Java program using Method Overriding
Using StringTokenizer in Java
How to use ArrayList in Java
Using substring( ) in Java
Java File
MultiLevel Inheritance sample in Java
Read a file line by line in Java - Sample Program
The clone() Method in Java