Runtime exec(String[], String[]) in Java

exec(String[], String[]): This method is available in the java.lang.Runtime class of Java.

Syntax:

Process java.lang.Runtime.exec(String[] cmdarray, String[] envp) throws IOException

This method takes two arguments. This method executes the specified command and arguments in a separate process with the specified environment.

Parameters: Two 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 environment variable settings in the format name=value, or null if the subprocess should inherit the environment 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. IOException - If an I/O error occurs.

3. NullPointerException - If cmdarray is null, or one of the elements of cmdarray is null or one of the elements of envp is null.

4. IndexOutOfBoundsException - If cmdarray is an empty array.

Approach 1: When no exception

Java

package com.Runtime;

import java.io.IOException;

public class Runtimeexec4 {
    public static void main(String[] args) throws IOException {

        Runtime runtime = Runtime.getRuntime();

        String cmdarray[] = { "notepad.exe" };
        String envp[] = { "notepad.exe" };

        System.out.println(runtime.exec(cmdarray, envp));
    }
}

Output:

Process[pid=7744, exitValue="not exited"]


Approach 2: IOException

Java

package com.Runtime;

import java.io.IOException;

public class Runtimeexec4 {
    public static void main(String[] args) throws IOException {

        Runtime runtime = Runtime.getRuntime();

        String cmdarray[] = { "hello.exe" };
        String envp[] = { "notepad.exe" };

        System.out.println(runtime.exec(cmdarray, envp));
    }
}

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 java.base/java.lang.Runtime.exec(Runtime.java:493) at com.Runtime.Runtimeexec4.main(Runtimeexec4.java:13) 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) ... 4 more


Approach 3: NullPointerException

Java

package com.Runtime;

import java.io.IOException;

public class Runtimeexec4 {
    public static void main(String[] args) throws IOException {

        Runtime runtime = Runtime.getRuntime();

        String cmdarray[] = null;
        String envp[] = { "notepad.exe" };

        System.out.println(runtime.exec(cmdarray, envp));
    }
}

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 java.base/java.lang.Runtime.exec(Runtime.java:493) at com.Runtime.Runtimeexec4.main(Runtimeexec4.java:13)


Approach 4: IndexOutOfBoundsException 

Java

package com.Runtime;

import java.io.IOException;

public class Runtimeexec4 {
    public static void main(String[] args) throws IOException {

        Runtime runtime = Runtime.getRuntime();

        String cmdarray[] = {};
        String envp[] = { "notepad.exe" };

        System.out.println(runtime.exec(cmdarray, envp));
    }
}

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 java.base/java.lang.Runtime.exec(Runtime.java:493) at com.Runtime.Runtimeexec4.main(Runtimeexec4.java:13)


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