How to Run a Java Program from Command Prompt
Running a Java program from the command prompt is a fundamental skill for any Java developer. It allows you to execute your code in a command-line interface, which is useful for debugging, automating tasks, and running scripts. In this article, we will guide you through the steps to run a Java program from the command prompt on Windows, macOS, and Linux.
1. Install Java Development Kit (JDK)
Before you can run a Java program from the command prompt, you need to have the Java Development Kit (JDK) installed on your system. The JDK includes the Java Runtime Environment (JRE) and the tools required to compile and run Java applications.
To install the JDK, visit the official Oracle website or a trusted third-party source and download the appropriate version for your operating system. Follow the installation instructions provided by the JDK installer.
2. Set JAVA_HOME Environment Variable
Once the JDK is installed, you need to set the JAVA_HOME environment variable to point to the JDK installation directory. This variable will be used by the command prompt to locate the Java executable.
On Windows, 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 ‘System variables’ section, scroll down and select ‘JAVA_HOME’.
5. Click ‘Edit’ and enter the path to your JDK installation directory.
6. Click ‘OK’ to save the changes.
On macOS and Linux, follow these steps:
1. Open a terminal window.
2. Use the ‘echo’ command to set the JAVA_HOME environment variable:
“`
export JAVA_HOME=/path/to/your/jdk/installation/directory
“`
3. Save the changes to your shell configuration file (e.g., `.bashrc` or `.zshrc`) by adding the following line:
“`
export JAVA_HOME=/path/to/your/jdk/installation/directory
“`
4. Restart your terminal or source the configuration file to apply the changes.
3. Set PATH Environment Variable
Next, you need to add the JDK’s ‘bin’ directory to the PATH environment variable. This will allow you to run Java commands from any command prompt.
On Windows, 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 ‘System variables’ section, scroll down and select ‘Path’.
5. Click ‘Edit’ and add the path to your JDK’s ‘bin’ directory.
6. Click ‘OK’ to save the changes.
On macOS and Linux, follow these steps:
1. Open a terminal window.
2. Use the ‘echo’ command to add the JDK’s ‘bin’ directory to the PATH environment variable:
“`
export PATH=$PATH:/path/to/your/jdk/installation/directory/bin
“`
3. Save the changes to your shell configuration file (e.g., `.bashrc` or `.zshrc`) by adding the following line:
“`
export PATH=$PATH:/path/to/your/jdk/installation/directory/bin
“`
4. Restart your terminal or source the configuration file to apply the changes.
4. Compile and Run Your Java Program
Now that you have set up the environment variables, you can compile and run your Java program from the command prompt.
1. Open a command prompt window.
2. Navigate to the directory containing your Java source file using the ‘cd’ command.
3. Compile your Java program using the ‘javac’ command, followed by the name of your source file (e.g., ‘javac MyClass.java’).
4. Run your Java program using the ‘java’ command, followed by the name of your compiled class (e.g., ‘java MyClass’).
Congratulations! You have successfully run a Java program from the command prompt. By following these steps, you can now execute your Java applications in a command-line interface, making it easier to manage and automate your development process.