How to Run Java Program on Command Prompt
Running a Java program on the command prompt is a fundamental skill for any Java developer. The command prompt provides a powerful and flexible environment for executing Java programs. In this article, we will guide you through the steps to run a Java program on the command prompt, ensuring that you have a smooth and efficient experience.
1. Install Java Development Kit (JDK)
Before you can run a Java program on the command prompt, you need to install the Java Development Kit (JDK). The JDK includes the Java Runtime Environment (JRE) and the tools required for developing Java applications. You can download the JDK from the official Oracle website or use an open-source alternative like OpenJDK.
2. Set the JAVA_HOME Environment Variable
Once you have installed the JDK, you need to set the JAVA_HOME environment variable. This variable tells the system where the JDK is installed. To set the JAVA_HOME environment variable, follow these steps:
1. Right-click on “This PC” or “Computer” and select “Properties.”
2. Click on “Advanced system settings.”
3. In the System Properties window, click on the “Environment Variables” button.
4. In the Environment Variables window, scroll down and select “JAVA_HOME.”
5. Click on “Edit” and enter the path to your JDK installation directory. For example, if you installed JDK in “C:\Program Files\Java\jdk-18.0.1”, enter “C:\Program Files\Java\jdk-18.0.1” as the value.
6. Click “OK” to save the changes.
3. Set the PATH Environment Variable
After setting the JAVA_HOME environment variable, 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 the command prompt. To set the PATH environment variable, follow these steps:
1. In the Environment Variables window, scroll down and select “Path.”
2. Click on “Edit” and click “New.”
3. Enter the path to the JDK’s “bin” directory, which is usually the same as the JAVA_HOME path but with “/bin” appended. For example, if your JAVA_HOME is “C:\Program Files\Java\jdk-18.0.1”, enter “C:\Program Files\Java\jdk-18.0.1\bin” as the value.
4. Click “OK” to save the changes.
4. Verify the Installation
To verify that your Java installation is successful, open the command prompt and type the following command:
“`
java -version
“`
If the command returns the version information of your Java installation, it means that the installation was successful.
5. Compile and Run Your Java Program
Now that you have set up the command prompt for running Java programs, you can compile and execute your Java code. Here’s how to do it:
1. Open the command prompt.
2. Navigate to the directory where your Java source file is located using the “cd” command. For example, if your file is in “C:\Java\MyProject”, type “cd C:\Java\MyProject” and press Enter.
3. Compile your Java source file using the “javac” command. For example, if your file is named “MyProgram.java”, type “javac MyProgram.java” and press Enter.
4. If the compilation is successful, you can run your Java program using the “java” command. For example, to run “MyProgram.java”, type “java MyProgram” and press Enter.
Congratulations! You have successfully run a Java program on the command prompt. With these steps, you can now execute your Java applications and explore the vast world of Java development.