Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Runtime Exec Example

 
 

Share on Google+Share on Google+

Runtime Exec Example

Advertisement
In this example we are going to execute an application using java program.

Runtime Exec Example

     

In this example we are going to execute an application using java program.

 This example uses the Process class that is contained in the lang package and extends Object class. The method Runtime.exec() creates a native process and returns an instance of a subclass of the Process class. This instance is then used to control the process and to obtain the information about the process. The Process class provides methods for performing input ,output , wait, checking the exit status , and destroying (killing) the process.

The Runtime.exec() method may not work well for windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts.

The class Runtime contained in java.lang package and extends the Object class. Every Java application has a single instance of class Runtime. The current runtime can be obtained by using the getRuntime() method. Any application cannot create its own instance of runtime class. 

The method used:
exec(String command):
This method is used to execute the command. Here command is a specified system command. It can also be used to execute any .exe file.  

 The code of the program is given below:

import java.io.IOException; 
public class RuntimeExec{
  public static void main(String[] args){
  try
  {
  Process process = Runtime.getRuntime()
.exec
("notepad.exe");
  }catch (IOException e)
  {
 e.printStackTrace();
  }
  }
}

The output of the program is given below:

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 6, 2007

Related Tutorials

Discuss: Runtime Exec Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Antoine DUNTZE
September 5, 2011
Runtime Exec Example

Hello ! I'm very new to java programming... My question is : in this example,"notepad.exe" is fixed. How to make a dynamic programm passing the name of the programm to run instead of "notepad.exe" ? Thanks for your answer. Antoine DUNTZE
mandar
February 17, 2013
runtime java application

How we can run any java program ,by using runtime api.
DMCA.com