Laptop running Java commands in Windows Command Prompt (javac and java) – 1200×628 DSLR-style image

Blog

November 25, 2025 11:53 am

How to Run a Java Program in Command Prompt (Windows Guide 2026)

Java is still one of the most widely used programming languages for beginners, students, and working professionals. If you want to run a Java program without installing any heavy IDE, using the Command Prompt (CMD) is the fastest and cleanest method. This guide explains what you need, how it works, when to use CMD, when to avoid it, and the exact steps with zero unnecessary words.

What Is Command Prompt Used for in Java?

The Windows Command Prompt lets you:

  • Compile Java files
  • Run Java programs
  • Set Java PATH
  • Quickly test simple code without an IDE

CMD is lightweight, fast, and ideal for learning the basics of Java execution.

When Should You Use CMD to Run Java Programs?

Good Use Cases

  • Learning core Java basics
  • Running small practice programs
  • Testing Java logic without IDE
  • Low-RAM laptops where IDEs lag

When to Avoid CMD

Avoid CMD for:

  • Large enterprise projects
  • Multi-file Java applications
  • GUI-based or Maven/Gradle projects

In such cases, full IDEs like IntelliJ IDEA or Eclipse are better.

Requirements Before Running Java in CMD

You must have:

  1. JDK (Java Development Kit) installed
  2. JAVA_HOME and PATH correctly set
  3. A Java file saved with .java extension

Step-by-Step Guide — How to Run a Java Program in Command Prompt

This is the clearest and most accurate method followed by professionals.

Step 1 — Install JDK

  • Download and install the latest JDK from Oracle or OpenJDK.
  • During installation, select “Add to PATH” if available.

Step 2 — Verify Installation

Open CMD and type:

java -version
javac -version

If both commands show a version, Java is installed correctly.

Step 3 — Create Your Java Program

Example: HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Save this file in any folder (e.g., Desktop or D Drive).

Step 4 — Open CMD in the File Location

Use one of these methods:

  • Navigate manually using the cd command
  • OR open CMD directly from File Explorer path bar by typing cmd

Example:

cd Desktop

Step 5 — Compile the Java File

Run:

javac HelloWorld.java

If no error appears, the program compiled successfully, and a new HelloWorld.class file is created.

Step 6 — Run the Program

Execute:

java HelloWorld

Output:

Hello, Java!

Common Errors & Quick Fixes

1. ‘javac’ is not recognized

Reason: Java PATH not added.
Fix: Add JDK’s bin folder to PATH via System Environment Variables.

2. Class name and filename mismatch

Example error:

class HelloWorld is public, should be declared in a file named HelloWorld.java

Fix: File name and class name must be identical.

3. Wrong directory

Fix: Ensure you are in the same folder where the Java file is saved.

Is Running Java in CMD Necessary?

Yes—if you want:

  • Clean understanding of how Java works internally
  • Fast execution without IDE load
  • Perfect practice setup for beginners

Not necessary for:

  • Advanced projects
  • Large frameworks
  • Android or Spring Boot development
How to Open Command Prompt in Windows (Fastest Methods 2026 Guide)
RELATED
How to Open Command Prompt in Windows (Fastest Methods 2026 Guide)

Final Summary

Running Java through Command Prompt is the simplest, lightest, and most beginner-friendly method. You only need:

  1. JDK
  2. PATH correctly set
  3. A .java file
  4. CMD commands: javac and java

This method is completely safe, standard, and recommended for learning.

Some more you might like

Copied!
⬇️

Leave a Comment

Floating Share – Bottom Arrow, Smaller Icons, Join Hides
M Raj - Founder of AskAIWorld

✨ Meet M Raj

Founder and editor of AskAIWorld.com — sharing how Artificial Intelligence makes creativity, productivity, and learning easier for everyone.

What started as a passion for AI-generated art has grown into exploring smart tools, automation, and content creation ideas that empower creators and web growlers alike.

View Author Page