How to Run a Java Program in Command Prompt
Running a Java program in the Command Prompt is a fundamental skill for any Java developer. The Command Prompt, also known as the Command Line Interface (CLI), allows you to execute Java programs directly from the terminal. In this article, we will guide you through the process of running a Java program in the Command Prompt on Windows, macOS, and Linux.
Setting Up the Java Development Kit (JDK)
Before you can run a Java program in the Command Prompt, you need to have the Java Development Kit (JDK) installed on your computer. The JDK includes the Java Runtime Environment (JRE) and the Java compiler (javac), which are necessary for compiling and running Java programs.
To install the JDK, visit the official Oracle website or the OpenJDK website and download the appropriate version for your operating system. Follow the installation instructions provided by the website to install the JDK.
Configuring the Environment Variable
After installing the JDK, you need to configure the environment variable to include the path to the JDK. This will allow the Command Prompt to locate the Java compiler and runtime environment.
1. On Windows, open the System Properties by right-clicking on “This PC” or “My Computer” and selecting “Properties”. Then, click on “Advanced system settings” and go to the “Environment Variables” tab.
2. In the “System variables” section, find the “Path” variable and click on “Edit”.
3. Add the path to the JDK’s “bin” directory to the end of the value. For example, if the JDK is installed in “C:\Program Files\Java\jdk-11.0.10”, you would add “;C:\Program Files\Java\jdk-11.0.10\bin” to the path.
4. Click “OK” to save the changes and close all the windows.
Writing a Java Program
Now that you have the JDK installed and the environment variable configured, you can write a simple Java program to test the setup. Create a new text file with a “.java” extension, for example, “HelloWorld.java”, and add the following code:
“`java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
“`
Compiling and Running the Java Program
To compile and run the Java program, open the Command Prompt and navigate to the directory where the “HelloWorld.java” file is located. Then, use the following commands:
1. Compile the Java program using the `javac` command:
“`
javac HelloWorld.java
“`
This will generate a “HelloWorld.class” file in the same directory.
2. Run the compiled Java program using the `java` command:
“`
java HelloWorld
“`
If everything is set up correctly, you should see the output “Hello, World!” displayed in the Command Prompt.
Conclusion
Running a Java program in the Command Prompt is a crucial skill for Java developers. By following the steps outlined in this article, you can set up your environment, write a Java program, and execute it from the Command Prompt. As you continue your journey in Java development, you will find that the Command Prompt is a powerful tool for debugging and managing your projects.