How to Run a Java Program from CMD
Running a Java program from the Command Prompt (CMD) is a fundamental skill for any Java developer. It allows you to execute your Java applications efficiently and effectively. In this article, we will guide you through the steps to run a Java program from CMD on Windows, macOS, and Linux operating systems.
1. Install Java Development Kit (JDK)
Before you can run a Java program from CMD, 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). You can download the JDK from the official Oracle website or use an open-source alternative like OpenJDK.
2. Set up the Java environment variable
To run Java programs from CMD, you need to set up the Java environment variable. This variable will allow your system to locate the JDK and execute Java commands.
On Windows:
– Open the System Properties window by right-clicking on “This PC” or “My Computer” and selecting “Properties.”
– Click on “Advanced system settings” and then “Environment Variables.”
– Under the “System variables” section, scroll down and find the “Path” variable. Click on “Edit.”
– In the “Edit Environment Variable” window, click “New” and add the path to your JDK’s “bin” directory. For example, if your JDK is installed in “C:\Program Files\Java\jdk-11.0.9”, add “C:\Program Files\Java\jdk-11.0.9\bin” to the list.
On macOS and Linux:
– Open a terminal window.
– Create a new file called “.bash_profile” in your home directory: `nano ~/.bash_profile`
– Add the following line to the file: `export PATH=$PATH:/path/to/your/jdk/bin`
– Save and close the file: `Ctrl + X`, then `Y`, then `Enter`.
– Source the new file to update your environment variables: `source ~/.bash_profile`
3. Compile your Java program
Once you have set up the Java environment variable, you can compile your Java program using the javac command. Open CMD and navigate to the directory containing your Java source file. Then, run the following command:
“`
javac YourProgram.java
“`
This command will compile your Java source file and generate a bytecode file named “YourProgram.class” in the same directory.
4. Run your Java program
After compiling your Java program, you can run it from CMD using the java command. In the same directory, type the following command:
“`
java YourProgram
“`
Replace “YourProgram” with the name of your compiled class file (without the “.class” extension). The command will execute your Java program, and you should see the output in the CMD window.
5. Troubleshooting common issues
If you encounter any issues while running your Java program from CMD, consider the following troubleshooting steps:
– Ensure that you have installed the JDK correctly and that the Java environment variable is set up properly.
– Verify that your Java source file has no syntax errors.
– Check if the class file is in the same directory as the CMD window.
– Make sure you are using the correct command to compile and run your program.
By following these steps, you should be able to run a Java program from CMD on any operating system. Happy coding!