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

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

Syntax:

Process java.lang.Runtime.exec(String command, String[] envp, File dir) throws IOException

This method takes three arguments. This method executes the specified string command in a separate process with the specified environment and working directory.

Parameters: Three parameters are required for this method.

command: a specified system command.

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.

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. IOException - If an I/O error occurs.

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

4. IllegalArgumentException - If the command is empty.

Approach 1: When no exception

Java

package com.Runtime;

import java.io.File;
import java.io.IOException;

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

        Runtime runtime = Runtime.getRuntime();

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

        File dir = null;

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

Output:

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


Approach 2: IOException

Java

package com.Runtime;

import java.io.File;
import java.io.IOException;

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

        Runtime runtime = Runtime.getRuntime();

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

        File dir = null;

        System.out.println(runtime.exec(command, 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 java.base/java.lang.Runtime.exec(Runtime.java:415) at com.Runtime.Runtimeexec5.main(Runtimeexec5.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) ... 4 more


Approach 3: NullPointerException 

Java

package com.Runtime;

import java.io.File;
import java.io.IOException;

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

        Runtime runtime = Runtime.getRuntime();

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

        File dir = null;

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

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "command" is null at java.base/java.lang.Runtime.exec(Runtime.java:408) at com.Runtime.Runtimeexec5.main(Runtimeexec5.java:16)


Approach 4: IllegalArgumentException

Java

package com.Runtime;

import java.io.File;
import java.io.IOException;

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

        Runtime runtime = Runtime.getRuntime();

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

        File dir = null;

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

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Empty command at java.base/java.lang.Runtime.exec(Runtime.java:409) at com.Runtime.Runtimeexec5.main(Runtimeexec5.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