![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- 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 Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Runtime exec() Method
Description
The Java Runtime exec(String command) method executes the specified string command in a separate process. This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null).
Declaration
Following is the declaration for java.lang.Runtime.exec() method
public Process exec(String command)
Parameters
command − a specified system command.
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
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. This will invoke the notepad 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 { // print a message System.out.println("Executing notepad.exe"); // create a process and execute notepad.exe Process process = Runtime.getRuntime().exec("notepad.exe"); // 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. This will invoke the calculator 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 { // print a message System.out.println("Executing calculator.exe"); // create a process and execute calc.exe Process process = Runtime.getRuntime().exec("calc.exe"); // 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 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 { // print a message System.out.println("Executing explorer.exe"); // create a process and execute explorer.exe Process process = Runtime.getRuntime().exec("explorer.exe"); // 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 explorer.exe Windows Explorer should now open.