Java Program to Open the Command Prompt and Insert Commands
Last Updated :
22 Apr, 2025
In Java, the Runtime class allows Java applications to interact with the system's environment. Every Java application has an object of this class, this class provides exec() method and this method is used to run system commands.
In this article, we will discuss how the exec() method can be used to open the command prompt and run commands.
Syntax of exec() Method
The syntax of exec() method is:
public Process exec(String command)
- Parameter: This method takes a single parameter, command, which is the command to be executed.
- Return Type: This method returns a process object that manages the subprocess.
- Exceptions: This method throws SecurityException, IOException, NullPointerException, and IllegalArgumentException
Opening the Command Prompt in Java
The very first step is to open the Command Prompt. It is done by passing "cmd" command to exec() method.
Example:
Java
// Java program to illustrate
// open cmd prompt
class Geeks
{
public static void main(String[] args)
{
try
{
// Just one line and you are done !
// We have given a command to start cmd
// /K : Carries out command specified by string
Runtime.getRuntime().exec(new String[] {"cmd", "/K", "Start"});
}
catch (Exception e)
{
System.out.println("An error occurred while executing the command.");
e.printStackTrace();
}
}
}
When we run this code, it will open a new Commond Prompt window. If there is any issue in the code, the exception will be printed which is "An error occurred while executing the command."
Note: This code should be executed on a local system. It will not work on an online IDE as it interacts directly with the operating system.
Output:
Running Commands in the Command Prompt
To execute the commands inside the command prompt we can use Runtime.exec() method. We can perform various commands such as dir, which is used to list all directories and the ping, which is used to test the ability of the source computer to reach a specified destination computer. Now, we will see how we can run multiple commands using exec() method.
Example:
Java
// Java program to illustrate
// executing commands on cmd prompt
class Geeks
{
public static void main(String[] args)
{
try
{
// We are running "dir" and "ping" command on cmd
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"dir && ping localhost\"");
}
catch (Exception e)
{
System.out.println("HEY Buddy ! U r Doing Something Wrong ");
e.printStackTrace();
}
}
}
Output:
Similar Reads
Calling an External Program in Java using Process and Runtime Java contains the functionality of initiating an external process - an executable file or an existing application on the system, such as Google Chrome or the Media Player- by simple Java code. One way is to use following two classes for the purpose: Process class Runtime class The Process class pres
2 min read
Create a New Maven Project from Command Prompt Maven is a powerful project management tool that is based on POM (project object model). It is used for projects build, dependency and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT.In short, maven is a tool that can be used for building and managing an
2 min read
How to Run the Java Code in Git Bash? Git Bash is a command-line interface that provides a Unix-like environment on Windows. You can use it to compile and run Java programs just as you would in traditional terminals on Linux or macOS. Step to Run Java Code in Git Bash:1. Install JavaMake sure you have Java installed on your Windows comp
2 min read
How to Create a .exe File From a Java Program? In this article, we will learn how to create a .exe file from a Java program. Java is a platform-independent language, which works on the philosophy of "write Once, Run Anywhere". It simply means that if we write a Java program, we can run it on any device that has a JVM(Java Virtual Machine). Now i
5 min read
What is Command Line Runner Interface in Spring Boot? Spring Boot CLI (Command Line Interface) is a command line software or tool. This tool is provided by the Spring framework for quickly developing and testing Spring Boot applications from the command prompt. In this article, we will discuss the command line runner interface in the spring boot. Sprin
3 min read
How to Install Java on Windows, Linux and macOS? Java is a versatile programming language widely used for building applications. To start coding in Java, you first need to install the Java Development Kit (JDK) on your system. This article provides detailed steps for installing Java on Windows 7, 8, 10, 11, Linux Ubuntu, and macOS.Download and Ins
5 min read