Running a Java program from the Windows Command Prompt (cmd) is a fundamental skill for any developer working with the language. This process connects your writing environment to the Java Runtime Environment, allowing you to execute code outside of an Integrated Development Environment. While it seems straightforward, mastering the nuances ensures your projects compile and run reliably every time.
Verifying Your Java Installation
Before you can run a Java program, you must confirm that the Java Development Kit (JDK) is correctly installed on your machine. The system needs to recognize the Java commands you type in the terminal. Without this step, you will encounter errors indicating that the command is not recognized.
Opening System Verification
To verify your installation, open the Command Prompt by pressing Win + R , typing cmd , and hitting Enter. In this window, type java -version and press Enter. If Java is installed, the console will display the version number. If you see an error, you need to add the JDK's bin directory to your system's PATH environment variable.
Compiling Your Java Source Code
Java is a compiled language, which means you must translate your human-readable code (.java files) into bytecode (.class files) that the Java Virtual Machine (JVM) can understand. This translation is handled by the javac compiler, which is included with the JDK.
Navigating to the Project Folder
Use the cd (change directory) command to navigate to the folder containing your Java file. For example, if your code is on the Desktop, you would type cd C:\Users\YourName\Desktop . Once you are in the correct directory, run javac HelloWorld.java . If the compilation is successful, no message appears, and a new .class file appears in the same folder.
Executing the Compiled Bytecode
After successful compilation, you are ready to run the program. This step involves invoking the JVM and telling it which class containing the main method to start executing. Note that you use the java command here, not javac , and you do not include the .class extension in the command.
Handling Package Declarations
If your Java file contains a package declaration (e.g., package com.myapp; ), you must run the command from the parent directory of that package. For instance, if your package is com.myapp , navigate to the folder that contains the com directory. Then, execute the program by typing java com.myapp.HelloWorld .
Passing Arguments and Troubleshooting
Advanced execution allows you to pass command-line arguments to your application directly from the cmd window. You simply list the arguments after the class name when you run the java command. Furthermore, understanding common errors is vital; "ClassNotFoundException" usually means you typed the class name incorrectly, while "NoClassDefFoundError" often indicates a problem with your classpath settings.
Command | Purpose
java -version | Checks if Java is installed and displays the version.
javac HelloWorld.java | Compiles the source code into bytecode.
java HelloWorld | Runs the compiled program (no .class extension).