Runtime Exec Example

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.

Runtime Exec Example

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.