Java Data Type Tutorial - Java Runtime.exec(String [] cmdarray, String [] envp, File dir)








Syntax

Runtime.exec(String [] cmdarray, String [] envp, File dir) has the following syntax.

public Process exec(String [] cmdarray,  String [] envp,  File dir)  throws IOException

Example

In the following code shows how to use Runtime.exec(String [] cmdarray, String [] envp, File dir) method.

/*from   ww  w. j  av  a  2  s  .c  om*/
import java.io.File;

public class Main {

   public static void main(String[] args) {
      try {
         String[] cmdArray = new String[2];
         // the program to open
         cmdArray[0] = "notepad.exe";

         // txt file to open with notepad
         cmdArray[1] = "test.txt";

         File dir = new File("c:/");

         String[] envArray = new String[2];
         envArray[0]="";
         envArray[1]="";
         
         Process process = Runtime.getRuntime().exec(cmdArray, envArray, dir);

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

   }
}