Javatpoint Logo
Javatpoint Logo

Java Program to open the command prompt and insert commands

Here, the Runtime class from the java.lang package will be used. Because every Java program has an instance of the Runtime class, this class enables Java applications to alter their executing environment. Let's look at the Runtime class exec() Method to see how the task might be carried out.

java.lang.Runtime.exec(String command): A key method in running the supplied string command is java.lang.Runtime.exec(String command). The java.lang.Runtime.exec(String command) method is a part of the Runtime class, which is found in the java.lang package of the Java programming language. The Method executes a specified string command as a separate process.

Syntax:

Parameters :

Command: The specific Command you intend to execute.

Returns :

A new Process object that allows you to manage the subprocess.

Throws:

SecurityException: The exception is thrown if a security manager exists and its checkExec method denies the creation of the subprocess due to security constraints.

IOException: The exception is thrown if an I/O error occurs while attempting to execute the Command.

NullPointerException: The exception is thrown if the provided Command is null.

IllegalArgumentException: The exception is thrown if the provided Command is an empty string.

How to run Command Prompt

Filename: OpenCommandPrompt.java

Output:

Java Program to open the command prompt and insert commands

Insert and run the Command.

You may run certain cmd commands using this code. The given program runs the cmd commands "dir" (list all directories) and "ping" (check the source computer's ability to connect to a given destination machine).

Filename: CommandExecutionExample.java

Output:

Java Program to open the command prompt and insert commands





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA