Java Program to Open the Command Prompt and Insert Commands Last Updated : 22 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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() MethodThe 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 IllegalArgumentExceptionOpening the Command Prompt in JavaThe 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 PromptTo 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: Comment More infoAdvertise with us Next Article Java Program to Open the Command Prompt and Insert Commands M Mohit Gupta_OMG Improve Article Tags : Java Practice Tags : Java 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 How to Set Java Path in Windows and Linux? PATH is an environment variable that is used by Operating System to locate the exe files (.exe) or java binaries ( java or javac command). The path once it is set, cannot be overridden. The PATH variable prevents us from having to write out the entire path to a program on the Command Line Interface 4 min read How to Setup IntelliJ IDEA For Java Competitive Programming Environment? IntelliJ IDEA is a popular Integrated Development Environment (IDE) used by many developers for Java programming. In this article, we'll walk through the steps to set up IntelliJ IDEA for competitive programming. Competitive programming is a popular activity among computer science students and profe 4 min read Setting up Java Competitive Programming Environment An operating system is required to be installed on your system. here we will be discussing the setup in windows. However, you can choose any operating system. Install JDK (Java Development Kit) JDK, is a program that allows you to write Java code from the comfort of your desktop. It contains a varie 5 min read Download and Install Java Development Kit (JDK) on Windows, Mac, and Linux Java Development Kit (JDK) is one of the most important tools for developers who use it to build, compile, and run Java applications. It does not matter if you are a beginner or an experienced programmer; installing JDK is the first step towards working with Java development. We can download JDK to 7 min read Like