Runtime exec(String) in Java

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

Syntax:

Process java.lang.Runtime.exec(String command) throws IOException

This method takes one argument. This method executes the specified string command in a separate process.

Parameters: One parameter is required for this method.

command: a specified system command.

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.

4. IllegalArgumentException - If the command is empty.

Approach 1: When no exception

Java

package com.Runtime;

import java.io.IOException;

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

        Runtime runtime = Runtime.getRuntime();

        String command = "notepad.exe";

        System.out.println(runtime.exec(command));
    }
}

Output:

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


Approach 2: NullPointerException 

Java

package com.Runtime;

import java.io.IOException;

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

        Runtime runtime = Runtime.getRuntime();

        String command = null;

        System.out.println(runtime.exec(command));
    }
}

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 java.base/java.lang.Runtime.exec(Runtime.java:312) at com.Runtime.Runtimeexec.main(Runtimeexec.java:12)


Approach 3: IllegalArgumentException 

Java

package com.Runtime;

import java.io.IOException;

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

        Runtime runtime = Runtime.getRuntime();

        String command = "";

        System.out.println(runtime.exec(command));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Empty command at java.base/java.lang.Runtime.exec(Runtime.java:409) at java.base/java.lang.Runtime.exec(Runtime.java:312) at com.Runtime.Runtimeexec.main(Runtimeexec.java:12)


Approach 4: IOException 

Java

package com.Runtime;

import java.io.IOException;

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

        Runtime runtime = Runtime.getRuntime();

        String command = "hello.txt";

        System.out.println(runtime.exec(command));
    }
}

Output:

Exception in thread "main" java.io.IOException: Cannot run program "hello.txt": 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 java.base/java.lang.Runtime.exec(Runtime.java:312) at com.Runtime.Runtimeexec.main(Runtimeexec.java:12) 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) ... 5 more


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