How to Run a Java Program in CMD: Step-by-Step Guide for Beginners
Introduction
If you’re brand new to learning Java, and you’re possibly thinking about how to run a java program in CMD, you’ve come to the right resource. Running Java from the command line isn’t just for hardcore developers; it’s a neat, and simple, but powerful skill that helps you better understand how Java runs behind the scenes.
Table Of Content
- Introduction
- Why Learn How to Run Java Program in CMD?
- Prerequisites Before Running Java in CMD
- Step 1 β Install Java (JDK)
- Step 2 β Set Java Environment Variables
- Step 3 β Write Your Java Program
- Step 4 β Compile the Java Program in CMD
- Step 5 β Run the Java Program in CMD
- How to Run Java Project in CMD (Multiple Files)
- Running Java from CMD on macOS/Linux
- Common Errors and Fixes
- Tips for Running Java in CMD
- Final Thoughts β How to Run a Java Program in CMD
- FAQ β How to Run Java Program in CMD
- Related Reads
This guide will show you how to run a java program on CMD, how to compile Java code and more importantly, how to run a Java project in CMD when you have multiple files. I’ll be covering everything you need to know step-by-step, and I will include some tips and tricks, some common errors and their solutions, and more.

Why Learn How to Run Java Program in CMD?
With many beginners beginning in an IDE like Eclipse or IntelliJ, they almost always skip the command line!
That said:
- Using the CMD (or Terminal) will give you greater control over your Java programs.
- CMD is quicker when compiling small test files and to run them!
- CMD teaches you how Java performs its compilation process without the IDE magic.
There are also other reasons for using CMD, like if you have a job interview it would be more impressive to your prospective employers if you can run java code in CMD.
Prerequisites Before Running Java in CMD

Before you start typing commands, make sure you have:
- Java Development Kit (JDK) installed
- Environment Variables set (for Windows)
- A text editor (Notepad, VS Code, Sublime Text, etc.)
- Basic knowledge of Java syntax
Step 1 β Install Java (JDK)
Windows:
- Download JDK from Oracle.
- Install it and note the installation path (e.g., C:\Program Files\Java\jdk-21).
macOS:
brew install openjdk
Linux (Ubuntu/Debian):
sudo apt install openjdk-21-jdk
Step 2 β Set Java Environment Variables
If you donβt do this, you might get the error:
‘javac’ is not recognized as an internal or external command
Windows:
- Search for “Environment Variables” in the Start Menu.
- Add JAVA_HOME with your JDK path.
- Add %JAVA_HOME%\bin to PATH.
Step 3 β Write Your Java Program
Letβs create a simple HelloWorld.java file.
public class HelloWorld {
Β Β Β public static void main(String[] args) {
Β Β Β Β Β Β Β System.out.println("Hello, CMD!");
Β Β Β }
}
Save it in a folder (e.g., C:\JavaProjects).
Step 4 β Compile the Java Program in CMD
Open Command Prompt, navigate to the folder:
cd C:\JavaProjects Compile the file: javac HelloWorld.java
If successful, this creates a HelloWorld.class file.
Step 5 β Run the Java Program in CMD
Now run the program:
java HelloWorld
Output:
Hello, CMD!
How to Run Java Project in CMD (Multiple Files)
If you have a Java project with multiple .java files:
javac *.java java MainClass
Replace MainClass with the file containing public static void main.
Running Java from CMD on macOS/Linux
On macOS/Linux, the steps are the same, but you open Terminal instead of CMD:
cd ~/JavaProjects javac HelloWorld.java java HelloWorld
Common Errors and Fixes
- ‘javac’ is not recognized β Set your environment variables.
- Could not find or load main class β Check if file/class names match.
- Compilation failed β Fix syntax errors before running.
Tips for Running Java in CMD
- Remember to always match the file name with the class name.
- Use the UP arrow key in CMD to quickly go back to a command.
- Organize your Java files into folders.
Final Thoughts β How to Run a Java Program in CMD
Knowing how to run a java program in CMD is not just for exam purposes, it’s one of the most important things for any Java developer to understand. Once you know how to run a java program in cmd and run a java project in cmd without using an IDE, you have real power over your code.
It doesn’t matter if you are a beginner or a professional, using java terminal commands will speed up debugging, improve understanding of Java, and enable you to work quickly on any OS. So go ahead and open your CMD or Terminal, and get to coding!
FAQ β How to Run Java Program in CMD
Q1: How to compile and run Java file in cmd?
A: Compile: javac FileName.java Run: java FileName
Q2: How to run Java project in cmd with packages?
A: Go to the src folder and enter javac -d . package/FileName.java then java package.FileName.
Q3: Can I run a Java without JDK?
A: No, you need the JDK to compile and run Java programs.
Related Reads
- Basic Programs on Java: Your First Step to Java Mastery π»
- Backend Java Developer Skills: 7 Must-Haves in 2025 π
- Object-Oriented Programming (OOP) In Java – Explained

