Running a Java program from the Command Prompt (CMD) is a fundamental skill every developer must master. While Integrated Development Environments (IDEs) like IntelliJ IDEA, Eclipse, or VS Code automate this process, understanding the underlying mechanics of compilation and execution builds a stronger foundation for debugging, build automation, and server-side deployment. This guide walks you through the complete workflow, from writing source code to troubleshooting common environment errors, ensuring you can run Java applications confidently on any Windows machine.
Prerequisites: Installing the JDK and Configuring Environment Variables
Before opening the Command Prompt, you must ensure the Java Development Kit (JDK) is installed and recognized by the operating system. The JDK contains the compiler (javac) and the runtime (java) required to build and run programs.
- Download the JDK: figure out to the official Oracle website or an OpenJDK distribution (like Eclipse Temurin or Microsoft Build of OpenJDK) and download the latest Long-Term Support (LTS) version installer for Windows (e.g., JDK 17 or JDK 21).
- Run the Installer: Follow the installation wizard. Take note of the installation path (default is usually
C:\Program Files\Java\jdk-<version>). You will need this path for the next step. - Set
JAVA_HOMEVariable:- Press the
Windows Key, type "Environment Variables," and select Edit the system environment variables. - Click the Environment Variables button.
- Under System variables, click New.
- Variable name:
JAVA_HOME - Variable value: Paste your JDK installation path (e.g.,
C:\Program Files\Java\jdk-21). - Click OK.
- Press the
- Update the
PathVariable:- In the System variables list, find
Path, select it, and click Edit. - Click New and add
%JAVA_HOME%\bin. - Click OK on all open dialogs.
- In the System variables list, find
- Verify Installation: Open a new Command Prompt window (existing windows will not pick up the changes) and type:
Both commands should return version numbers without errors. If you seejava -version javac -version'java' is not recognized as an internal or external command, re-check thePathvariable for typos.
Writing Your First Java Source File
Create a dedicated folder for your Java projects to keep files organized. For this tutorial, create a folder named JavaProjects on your C: drive or within your Documents folder.
- Open Notepad (or Notepad++ / VS Code without extensions).
- Paste the following standard "Hello World" code. Critical Rule: The filename must match the
public classname exactly, including capitalization.public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Command Line!"); } } - Save the file as
HelloWorld.javainside yourJavaProjectsfolder.- In Notepad, select Save as type: All Files to prevent it from saving as
HelloWorld.java.txt. - Ensure Encoding is set to UTF-8.
- In Notepad, select Save as type: All Files to prevent it from saving as
The Two-Step Process: Compile and Run
Java is a compiled language. Unlike Python or JavaScript, you cannot run the .In practice, java file directly. Here's the thing — you must first translate the human-readable source code into bytecode (. class files) that the Java Virtual Machine (JVM) understands Most people skip this — try not to. Surprisingly effective..
Step 1: Compilation (javac)
Open CMD and deal with to your project directory using the cd (change directory) command Easy to understand, harder to ignore..
cd C:\JavaProjects
Tip: You can type cd C:\JavaP and press Tab to auto-complete the folder name.
Once inside the directory, run the compiler:
javac HelloWorld.java
What happens here?
- The
javactool readsHelloWorld.java. - It checks for syntax errors.
- If successful, it generates a new file:
HelloWorld.class. This file contains platform-independent bytecode. - No output in the console means success. If errors appear, read them carefully—they usually indicate the line number and nature of the syntax mistake.
Step 2: Execution (java)
Now that the bytecode exists, invoke the JVM to run it. Crucial: Do not include the .class extension when running the command Easy to understand, harder to ignore..
java HelloWorld
Expected Output:
Hello, Command Line!
Congratulations. You have successfully compiled and executed a Java program using only the command line.
Understanding the Command Structure Deeply
To move beyond "Hello World," you need to understand how to manage packages, external libraries, and output directories.
Working with Packages
Real-world applications use packages to organize classes (e.g., com.Practically speaking, example. Worth adding: app). This changes the directory structure and commands The details matter here..
- Directory Structure: Create folders matching the package name.
mkdir com\example\app - Source Code: Update
HelloWorld.javato include a package declaration at the very top.package com.example.app; public class HelloWorld { public static void main(String[] args) { System.out.println("Running from a package!Even so, "); } } - Compile from Root: handle to the root folder (the parent of
com) and compile using the relative path. So naturally, ```cmd javac com/example/app/HelloWorld. javaThis preserves the package structure inside the generated `.class` files. - And Run from Root: Use the fully qualified class name (package + class) separated by dots. ```cmd java com.example.app.
Managing Output with -d Flag
By default, javac places .class files next to .java files. This clutters your source directory. Use the -d (destination) flag to separate compiled output Which is the point..
javac -d out com/example/app/HelloWorld.java
This creates an out folder containing the mirrored package structure: out/com/example/app/HelloWorld.class.
To run it, you must tell the JVM where to find the class files using the -cp (classpath) flag:
java -cp out com.example.app.HelloWorld
Handling External Libraries (JARs)
Most projects depend on third-party libraries (e.Think about it: g. Plus, , Gson, Jackson, database drivers). You must include these JAR files in the classpath during both compilation and execution That's the whole idea..
Assume you have a lib folder containing gson-2.10.1.jar.
Compile:
javac -cp "lib/gson-2.10.1.jar" -d out com/example/app/HelloWorld.java
- Windows Note: Use semicolons
;to separate multiple paths in-cp. Use quotes""if paths contain spaces.
Run:
java -cp "out;lib/gson-2.10.1.jar" com.example.app.HelloWorld
The classpath now includes both your compiled code (out) and the library (lib/...) Turns out it matters..
Passing Command-Line Arguments
The main method accepts a String[] args parameter. The result? You get to pass data into the program at runtime The details matter here..
Modify your main method:
public static void main(String[] args) {
if (args.length > 0) {
System.out.
Modify your main method so that it can react to the values supplied at runtime:
```java
public static void main(String[] args) {
System.out
```java
System.out.println("Arguments received: " + args.length);
for (int i = 0; i < args.length; i++) {
System.out.println(" [" + i + "] " + args[i]);
}
}
The args array contains every token passed after the class name on the command line. Compile and run the program with sample data:
javac -d out com/example/app/HelloWorld.java
java -cp out com.example.app.HelloWorld Alice Bob 21
Output:
Arguments received: 3
[0] Alice
[1] Bob
[2] 21
If an argument contains spaces, wrap it in quotes so the shell treats it as a single value:
java -cp out com.example.app.HelloWorld "Java Developer"
Conclusion
You now have a complete foundation for building and running Java programs from the command line. You learned how to organize code into packages, compile with javac while preserving directory structure, use the -d flag to keep compiled output separate from source, manage third-party dependencies through the classpath, and accept runtime input via String[] args. These concepts apply whether you are writing simple utilities or laying the groundwork for larger applications, and they remain essential even when you later migrate to build tools like Maven or Gradle.