How to Execute Java Program in CMD
In this article, we will discuss how to execute a Java program in the Command Prompt (CMD). CMD is a powerful tool for running various commands and scripts, and it is also commonly used for executing Java programs. By following these simple steps, you can easily run your Java program from the comfort of your command line interface.
Step 1: Install Java Development Kit (JDK)
Before you can execute a Java program in CMD, you need to have the Java Development Kit (JDK) installed on your computer. The JDK includes the Java Runtime Environment (JRE) and the tools required for compiling and running Java programs. You can download the JDK from the official Oracle website or from other trusted sources.
Step 2: Set up the JAVA_HOME environment variable
Once you have installed the JDK, you need to set up the JAVA_HOME environment variable. This variable tells the system where the JDK is installed. To set up the JAVA_HOME environment variable, follow these steps:
1. Right-click on “This PC” or “My Computer” and select “Properties.”
2. Click on “Advanced system settings” and then click on the “Environment Variables” button.
3. In the “System variables” section, scroll down and select “JAVA_HOME.” Click on “Edit.”
4. In the “Edit Environment Variable” window, enter the path to your JDK installation directory (e.g., C:\Program Files\Java\jdk1.8.0_251) and click “OK.”
5. Click “OK” on the remaining windows to close them.
Step 3: Set up the PATH environment variable
Next, you need to add the JDK’s “bin” directory to the PATH environment variable. This allows you to run Java commands from any directory in CMD. To do this, follow these steps:
1. In the “Environment Variables” window, scroll down and select “Path.”
2. Click on “Edit.”
3. Click “New” and add the path to the JDK’s “bin” directory (e.g., C:\Program Files\Java\jdk1.8.0_251\bin) to the list of variables.
4. Click “OK” on the remaining windows to close them.
Step 4: Open CMD and run your Java program
Now that you have set up the environment variables, you can open CMD and run your Java program. To do this, follow these steps:
1. Open CMD by searching for “cmd” in the Start menu or by pressing the Windows key + R, typing “cmd,” and pressing Enter.
2. Navigate to the directory where your Java program is located using the “cd” command. For example, if your program is in the “C:\JavaProjects” directory, type “cd C:\JavaProjects” and press Enter.
3. Compile your Java program using the “javac” command. For example, if your program’s name is “MyProgram.java,” type “javac MyProgram.java” and press Enter.
4. Run your compiled Java program using the “java” command. For example, if your program’s name is “MyProgram,” type “java MyProgram” and press Enter.
Congratulations! You have successfully executed your Java program in CMD. By following these steps, you can easily run your Java programs from the command line interface, which can be particularly useful for automating tasks or running scripts.