exec(String[], String[], File): This method is available in the java.lang.Runtime class of Java.
Syntax:
Process java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir) throws IOException
This method takes three arguments. This method executes the specified command and arguments in a separate process with the specified environment and working directory.
Parameters: Three parameters are required for this method.
cmdarray: an array containing the command to call and its arguments.
envp: array of strings, each element of which has an 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.
Returns: A new Process object for managing the subprocess.
Throws:
1. SecurityException - If a security manager exists and its checkExec method doesn't allow the creation of the subprocess.
2. UnsupportedOperationException - If the operating system does not support the creation of processes.
3. IOException - If an I/O error occurs.
4. NullPointerException - If cmdarray is null or one of the elements of cmdarray is null, or one of the elements of envp is null.
5. IndexOutOfBoundsException - If cmdarray is an empty array.
Approach 1: When no exception
Java
package com.Runtime;import java.io.File;import java.io.IOException;public class Runtimeexec6 {public static void main(String[] args) throws IOException {Runtime runtime = Runtime.getRuntime();String cmdarray[] = { "notepad.exe" };String envp[] = { "notepad.exe" };File dir = null;System.out.println(runtime.exec(cmdarray, envp, dir));}}
Output:
Process[pid=12408, exitValue="not exited"]
Approach 2: IOException
Java
package com.Runtime;import java.io.File;import java.io.IOException;public class Runtimeexec6 {public static void main(String[] args) throws IOException {Runtime runtime = Runtime.getRuntime();String cmdarray[] = { "hello.exe" };String envp[] = { "notepad.exe" };File dir = null;System.out.println(runtime.exec(cmdarray, envp, dir));}}
Output:
Exception in thread "main" java.io.IOException: Cannot run program "hello.exe": CreateProcess error=2, The system cannot find the file specified at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1142) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073) at java.base/java.lang.Runtime.exec(Runtime.java:591) at com.Runtime.Runtimeexec6.main(Runtimeexec6.java:16) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.base/java.lang.ProcessImpl.create(Native Method) at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:483) at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:158) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1109) ... 3 more
Approach 3: NullPointerException
Java
package com.Runtime;import java.io.File;import java.io.IOException;public class Runtimeexec6 {public static void main(String[] args) throws IOException {Runtime runtime = Runtime.getRuntime();String cmdarray[] = null;String envp[] = { "notepad.exe" };File dir = null;System.out.println(runtime.exec(cmdarray, envp, dir));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "command" is null at java.base/java.lang.ProcessBuilder.<init>(ProcessBuilder.java:229) at java.base/java.lang.Runtime.exec(Runtime.java:588) at com.Runtime.Runtimeexec6.main(Runtimeexec6.java:16)
Approach 4: IndexOutOfBoundsException
Java
package com.Runtime;import java.io.File;import java.io.IOException;public class Runtimeexec6 {public static void main(String[] args) throws IOException {Runtime runtime = Runtime.getRuntime();String cmdarray[] = {};String envp[] = { "notepad.exe" };File dir = null;System.out.println(runtime.exec(cmdarray, envp, dir));}}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1094) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073) at java.base/java.lang.Runtime.exec(Runtime.java:591) at com.Runtime.Runtimeexec6.main(Runtimeexec6.java:16)
Some other methods of Runtime
addShutdownHook(Thread): This method registers a new virtual machine shutdown hook.
availableProcessors(): This method returns the number of processors available to the Java virtual machine.
exec(String): This method executes the specified string command in a separate process.
exec(String[]): This method executes the specified command and arguments in a separate process.
exec(String, String[]): This method executes the specified string command in a separate process with the specified environment.
exec(String[], String[]): This method executes the specified command and arguments in a separate process with the specified environment.
exec(String, String[], File): This method executes the specified string command in a separate process with the specified environment and working directory.
exec(String[], String[], File): This method executes the specified command and arguments in a separate process with the specified environment and working directory.
gc(): This method runs the garbage collector in the Java Virtual Machine.
Runtime.getRuntime(): This method returns the runtime object associated with the current Java application.
halt(int): This method forcibly terminates the currently running Java virtual machine.
load(String): This method loads the native library specified by the filename argument.
loadLibrary(String): This method loads the native library specified by the libname argument.
maxMemory(): This method returns the maximum amount of memory that the Java virtual machine will attempt to use.
removeShutdownHook(Thread): This method De-registers a previously registered virtual machine shutdown hook.
runFinalization(): This method runs the finalization methods of any objects pending finalization.
version(): This method returns the version of the Java Runtime Environment as a Version.
exit(int): This method terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally.
freeMemory(): This method returns the amount of free memory in the Java Virtual Machine.
totalMemory(): This method returns the total amount of memory in the Java virtual machine.
No comments:
Post a Comment