Java Runtime exec() Method



Description

The Java Runtime exec(String command, String[] envp, File dir) method executes the specified string command in a separate process with the specified environment and working directory. This is a convenience method. An invocation of the form exec(command, envp, dir) behaves in exactly the same way as the invocation exec(cmdarray, envp, dir), where cmdarray is an array of all the tokens in command.

More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.

Declaration

Following is the declaration for java.lang.Runtime.exec() method

public Process exec(String command, String[] envp, File dir)

Parameters

  • command − a specified system command.

  • envp − array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.

  • dir − the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.

Return Value

This method returns a new Process object for managing the subprocess

Exception

  • SecurityException − If a security manager exists and its checkExec method doesn't allow creation of the subprocess

  • IOException − If an I/O error occurs

  • NullPointerException − If command is null, or one of the elements of envp is null

  • IllegalArgumentException − If command is empty

Example: Opening a Notepad Application

The following example shows the usage of Java Runtime exec() method. We've created a Process object for notepad executable using exec() method in current directory. This will invoke the notepad application. If some exception occurs, a corresponding stack trace is printed with error message.

package com.tutorialspoint;

import java.io.File;

public class RuntimeDemo {

   public static void main(String[] args) {
      try {

      // print a message
      System.out.println("Executing notepad.exe...");

      // create a file with the working directory we wish
      File dir = new File("C:/");

      // create a process and execute notepad.exe and currect environment
      Process process = Runtime.getRuntime().exec("notepad.exe", null, dir);

      // print another message
      System.out.println("Notepad should now open.");

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Executing notepad.exe...
Notepad should now open.

Example: Opening a Calculator Application

The following example shows the usage of Java Runtime exec() method. We've created a Process object for calculator executable using exec() method in current directory. This will invoke the calculator application. If some exception occurs, a corresponding stack trace is printed with error message.

package com.tutorialspoint;

import java.io.File;

public class RuntimeDemo {

   public static void main(String[] args) {
      try {

      // print a message
      System.out.println("Executing calc.exe...");

      // create a file with the working directory we wish
      File dir = new File("C:/");

      // create a process and execute calculator.exe and currect environment
      Process process = Runtime.getRuntime().exec("calc.exe", null, dir);

      // print another message
      System.out.println("Calculator should now open.");

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Executing calc.exe...
Calculator should now open.

Example: Opening a Windows Explorer Application

The following example shows the usage of Java Runtime exec() method. We've created a Process object for Windows Explorer executable using exec() method in current directory. This will invoke the Windows Explorer application. If some exception occurs, a corresponding stack trace is printed with error message.

package com.tutorialspoint;

import java.io.File;

public class RuntimeDemo {

   public static void main(String[] args) {
      try {

      // print a message
      System.out.println("Executing explorer.exe...");

      // create a file with the working directory we wish
      File dir = new File("C:/");

      // create a process and execute calculator.exe and currect environment
      Process process = Runtime.getRuntime().exec("explorer.exe", null, dir);

      // print another message
      System.out.println("Windows Explorer should now open.");

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Executing calc.exe...
Calculator should now open.
java_lang_runtime.htm
Advertisements