GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Java Runtime exec() Method

java package tutorial
  • Java.lang - Home
  • Java.lang - Boolean
  • Java.lang - Byte
  • Java.lang - Character
  • Java.lang - Character.Subset
  • Java.lang - Character.UnicodeBlock
  • Java.lang - Class
  • Java.lang - ClassLoader
  • Java.lang - Compiler
  • Java.lang - Double
  • Java.lang - Enum
  • Java.lang - Float
  • Java.lang - InheritableThreadLocal
  • Java.lang - Integer
  • Java.lang - Long
  • Java.lang - Math
  • Java.lang - Number
  • Java.lang - Object
  • Java.lang - Package
  • Java.lang - Process
  • Java.lang - ProcessBuilder
  • Java.lang - Runtime
  • Java.lang - RuntimePermission
  • Java.lang - SecurityManager
  • Java.lang - Short
  • Java.lang - StackTraceElement
  • Java.lang - StrictMath
  • Java.lang - String
  • Java.lang - StringBuffer
  • Java.lang - StringBuilder
  • Java.lang - System
  • Java.lang - Thread
  • Java.lang - ThreadGroup
  • Java.lang - ThreadLocal
  • Java.lang - Throwable
  • Java.lang - Void
  • Java.lang - Interfaces
  • Java.lang - Errors
  • Java.lang - Exceptions
  • Java.lang Package Useful Resources
  • Java.lang - Useful Resources
  • Java.lang - Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Java Runtime exec() Method



Previous
Quiz
Next

Description

The Java Runtime exec(String[] cmdarray) method executes the specified command and arguments in a separate process. This is a convenience method. An invocation of the form exec(cmdarray) behaves in exactly the same way as the invocation exec(cmdarray, null, null).

Declaration

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

public Process exec(String[] cmdarray)

Parameters

cmdarray − array containing the command to call and its arguments.

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

  • IndexOutOfBoundsException − If cmdarray is an empty array (has length 0)

Example

This example requires a file named example.txt in our CLASSPATH with the following contents −

Hello World!

Example: Opening a Notepad Application with Given Text File

The following example shows the usage of Java Runtime exec() method. In this program, we've a string array. Notepad.exe and test.txt entries are added. We've created a Process object for notepad executable using exec() method. This will invoke the notepad application and test.txt will be opened. If some exception occurs, a corresponding stack trace is printed with error message. If test.txt is not present in current classpath, then Notepad will report error for non-existing file.

package com.tutorialspoint;

public class RuntimeDemo {

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

         // create a new array of 2 strings
         String[] cmdArray = new String[2];

         // first argument is the program we want to open
         cmdArray[0] = "notepad.exe";

         // second argument is a txt file we want to open with notepad
         cmdArray[1] = "example.txt";

         // print a message
         System.out.println("Executing notepad.exe and opening example.txt");

         // create a process and execute cmdArray
         Process process = Runtime.getRuntime().exec(cmdArray);

         // print another message
         System.out.println("example.txt 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 and opening example.txt
example.txt should now open.

Example: Opening a Calculator Application

The following example shows the usage of Java Runtime exec() method. In this program, we've a string array. calc.exe entry is added. We've created a Process object for notepad executable using exec() method. This will invoke the calc application. If some exception occurs, a corresponding stack trace is printed with error message.

package com.tutorialspoint;

public class RuntimeDemo {

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

         // create a new array of 1 string
         String[] cmdArray = new String[1];

         // first argument is the program we want to open
         cmdArray[0] = "calc.exe";

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

         // create a process and execute cmdArray
         Process process = Runtime.getRuntime().exec(cmdArray);

         // print another message
         System.out.println("example.txt 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

Example: Opening a Windows Explorer Application

The following example shows the usage of Java Runtime exec() method. In this program, we've a string array. explorer.exe entry is added. We've created a Process object for explorer executable using exec() method. This will invoke the explorer application. If some exception occurs, a corresponding stack trace is printed with error message.

package com.tutorialspoint;

public class RuntimeDemo {

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

         // create a new array of 1 string
         String[] cmdArray = new String[1];

         // first argument is the program we want to open
         cmdArray[0] = "explorer.exe";

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

         // create a process and execute cmdArray
         Process process = Runtime.getRuntime().exec(cmdArray);

         // print another message
         System.out.println("example.txt should now open.");
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Executing explorer.exe
java_lang_runtime.htm
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.