How to Execute a Java Program in Command Prompt
Executing 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 interact with your computer through text-based commands. This article will guide you through the steps to execute a Java program in the Command Prompt on Windows, macOS, and Linux operating systems.
1. Install Java on Your System
Before you can execute a Java program in the Command Prompt, you need to have Java installed on your system. You can download the Java Development Kit (JDK) from the official Oracle website or use an open-source alternative like OpenJDK. Follow the installation instructions provided by the JDK package to install it on your computer.
2. Set the JAVA_HOME Environment Variable
After installing Java, you need to set the JAVA_HOME environment variable to specify the location of the JDK on your system. This variable is used by the Command Prompt to locate the Java executable. To set the JAVA_HOME variable, follow these steps:
– On Windows:
1. Right-click on ‘This PC’ or ‘Computer’ on the desktop or in File Explorer.
2. Select ‘Properties’.
3. Click on ‘Advanced system settings’.
4. In the System Properties window, click on the ‘Environment Variables’ button.
5. In the ‘System variables’ section, scroll down and select ‘JAVA_HOME’.
6. Click ‘Edit’, and enter the path to the JDK directory (e.g., C:\Program Files\Java\jdk-
7. Click ‘OK’ to save the changes.
– On macOS and Linux:
1. Open a terminal window.
2. Edit the ‘.bash_profile’ file (or ‘.bashrc’ on some systems) by running the following command:
“`
nano ~/.bash_profile
“`
3. Add the following line to the file:
“`
export JAVA_HOME=/path/to/jdk
“`
4. Save and close the file.
5. Source the file by running the following command:
“`
source ~/.bash_profile
“`
3. Set the PATH Environment Variable
The PATH environment variable is used by the Command Prompt to locate executable files. To add the JDK’s ‘bin’ directory to the PATH variable, follow these steps:
– On Windows:
1. Open the ‘Environment Variables’ window (as described in step 2 of the previous section).
2. In the ‘System variables’ section, scroll down and select ‘Path’.
3. Click ‘Edit’, and add the following line to the end of the list:
“`
%JAVA_HOME%\bin
“`
4. Click ‘OK’ to save the changes.
– On macOS and Linux:
1. Open the ‘.bash_profile’ file (as described in step 2 of the previous section).
2. Add the following line to the file:
“`
export PATH=$PATH:$JAVA_HOME/bin
“`
3. Save and close the file.
4. Source the file by running the following command:
“`
source ~/.bash_profile
“`
4. Compile and Execute Your Java Program
Now that you have Java installed and the necessary environment variables set, you can compile and execute your Java program in the Command Prompt. Follow these steps:
– Open the Command Prompt.
– Navigate to the directory containing your Java source file using the ‘cd’ command (e.g., `cd C:\path\to\your\java\program`).
– Compile your Java program using the ‘javac’ command followed by the filename (e.g., `javac MyClass.java`).
– Execute the compiled Java program using the ‘java’ command followed by the class name (e.g., `java MyClass`).
Congratulations! You have successfully executed a Java program in the Command Prompt. Continue exploring the Command Prompt and its capabilities to enhance your Java development experience.