How to Compile and Run a Java Program in CMD, VS Code and Notepad
To run a Java program, install the JDK, save your file as ClassName.java, open a terminal, run javac ClassName.java to compile, then run java ClassName to execute. The whole process takes under ten minutes once your PATH is set correctly.
Key things to know before you start:
- JDK is non-negotiable. The JRE alone won’t give you
javac. Download the full JDK from Oracle or Adoptium. - File name must match the class name exactly, including capitalisation. This single rule trips up more beginners than anything else.
- Two commands, two jobs:
javaccompiles source code into bytecode;javaruns that bytecode. - VS Code is the fastest modern setup for beginners, thanks to the Extension Pack for Java.
- Notepad works fine for learning, as long as you save the file as
.java, not.txt.
Installing the JDK and Setting the PATH
Java runs on the Java Virtual Machine, and the JVM needs the Java Development Kit behind it. As of 2024, Oracle’s JDK 21 is the current Long-Term Support release and is free for personal and development use. According to the JetBrains Developer Ecosystem Survey 2023, Java 17 and Java 11 are still the most widely deployed versions in production, but for fresh learners, start with JDK 21.
Download the installer from oracle.com/java or adoptium.net. Adoptium is fully open-source and is widely used in Indian engineering colleges, NPTEL lab environments and IT firms. Run the installer and note the installation path, usually C:\Program Files\Java\jdk-21 on Windows.
Setting PATH on Windows
Open System Properties, go to Advanced, click Environment Variables. Under System Variables, find Path, click Edit, then add a new entry pointing to the bin folder inside your JDK directory, for example C:\Program Files\Java\jdk-21\bin. Click OK on every dialog, then open a fresh Command Prompt and type javac -version. If it prints a version number, you are done.
If CMD still says “javac is not recognised as an internal or external command”, the PATH entry is wrong or you opened the terminal before saving the change. Close CMD completely, reopen it, and try again.
Setting PATH on macOS
On macOS, open Terminal and run java -version first. macOS Ventura and later often prompt you to install the JDK automatically. If not, download the macOS installer from Adoptium. Then add export JAVA_HOME=$(/usr/libexec/java_home) and export PATH=$JAVA_HOME/bin:$PATH to your ~/.zshrc file. Run source ~/.zshrc and verify with javac -version.
JDK Version Quick Reference
| JDK Version | Release Type | Support Until | Recommended For |
|---|---|---|---|
| JDK 8 | LTS | March 2030 (Oracle) | Legacy enterprise projects |
| JDK 11 | LTS | September 2026 | Production apps, Android |
| JDK 17 | LTS | September 2029 | Most enterprise teams today |
| JDK 21 | LTS | September 2031 | New learners and projects |
Source: Oracle Java SE Support Roadmap, 2024.
How to Compile and Run a Java Program in CMD
Before anything else, understand the rule that causes the majority of beginner errors: the file name must exactly match the public class name, including capitalisation. If your class is public class HelloWorld, the file must be saved as HelloWorld.java. Not helloworld.java. Exact match, every time.
Here is a minimal working program to test your setup:
File: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save the file to a simple path with no spaces, like C:\JavaProjects\. Spaces in folder names cause headaches with classpath resolution.
The javac Command (Compiling)
Open Command Prompt and navigate to your folder using cd C:\JavaProjects. Then run:
javac HelloWorld.java
If it succeeds, you will see no output at all, and a new file called HelloWorld.class will appear in the same folder. That .class file contains bytecode that the JVM executes.
The java Command (Executing)
Now run the program with:
java HelloWorld
No .java extension, no .class extension. Just the class name. If you type java HelloWorld.class, you will get the error “could not find or load main class” because the JVM treats the dot as a package separator.
Fixing Common CMD Errors When Running a Java Program
“javac is not recognised” means the PATH is not set or the terminal was not restarted after setting it. Go back to the PATH setup steps above.
“could not find or load main class” usually means you ran java HelloWorld.class instead of java HelloWorld, or you are running the command from the wrong directory. Always cd into the folder where the .class file lives before running java.
“class HelloWorld is public, should be declared in a file named HelloWorld.java” means the file name does not match the class name. Rename the file and try again.
According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, with strong adoption across Indian IT services firms including TCS, Infosys and Wipro. Getting comfortable with CMD compilation is a foundational skill for any Java developer role.
How to Run a Java Program in VS Code, an IDE or Notepad
The command-line approach teaches you what is actually happening under the hood. But for day-to-day work, most developers use an IDE or a modern editor. Each option has real trade-offs.
How to Run a Java Program in VS Code
VS Code is lightweight, free and works on Windows, macOS and Linux. Install the Extension Pack for Java from Microsoft directly inside VS Code’s Extensions panel. It bundles six extensions including Language Support for Java by Red Hat, the Debugger for Java and the Test Runner.
Once installed, open your .java file. A Run button appears above the main method automatically. Click it. VS Code compiles and runs the file in one step. You can also press F5 to run with the debugger attached, which shows you variable values at every step.
Students who want structured mentorship alongside tools like VS Code often benefit from bootcamp training programs that combine live instruction with hands-on labs.
Using IntelliJ IDEA
IntelliJ IDEA Community Edition is free and widely regarded as the best Java IDE available. According to the JetBrains Developer Ecosystem Survey 2023, IntelliJ IDEA is the primary IDE for 48% of Java developers worldwide. It handles project setup, imports, refactoring and debugging better than VS Code for large codebases.
How to Write and Run a Java Program in Notepad
Open Notepad, write your program, then go to File, Save As. In the “Save as type” dropdown, choose All Files, not “Text Documents”. Name the file HelloWorld.java. If you leave the type as Text Documents, Windows silently saves it as HelloWorld.java.txt and javac won’t find it. Compile and run from CMD exactly as described above.
If you want to go further with tools and version control, the GitHub Education program gives students free access to professional developer tools, which pairs well with any Java learning path.
Editor Comparison at a Glance
| Tool | Best For | Auto-compile | Debugger | Cost |
|---|---|---|---|---|
| CMD + Notepad | Learning fundamentals | No | No | Free |
| VS Code | Beginners to intermediate | Yes (with extension) | Yes | Free |
| IntelliJ IDEA Community | Serious Java development | Yes | Yes | Free |
| IntelliJ IDEA Ultimate | Enterprise and web projects | Yes | Yes | Paid (student discount available) |
Once you are comfortable running Java programs locally, the next step is learning how to structure multi-class projects, work with packages and use build tools like Maven or Gradle. The REACH learner community at 3.0 University is a good place to ask questions and get feedback from peers who have been through exactly the same learning curve.
Java skills are directly relevant to careers in backend development, Android development and enterprise software. The AI job market in 2025 increasingly values engineers who can write production-grade code in mature languages like Java. The Stack Overflow Developer Survey 2024 reports that Java developers earn a median salary of $70,000 globally, with senior roles in India at top-tier firms reaching INR 25-40 LPA.
If you want a structured path into programming and technology careers, explore the online certification courses at 3.0 University, covering Cybersecurity, Ethical Hacking, AI, Blockchain and Web3. The 3.0 University blog also publishes practical guides on tools, career paths and emerging technologies worth bookmarking.
Frequently Asked Questions
How do I run a Java program?
Install the JDK, write your code in any text editor, save the file as ClassName.java where the name matches your public class exactly, open a terminal in that folder, run javac ClassName.java to compile, then run java ClassName (no extension) to execute it. VS Code with the Extension Pack for Java simplifies this to a single click.
How do I compile a Java program in CMD?
Open Command Prompt, navigate to the folder containing your .java file using cd, then type javac FileName.java and press Enter. If the JDK PATH is set correctly and the file name matches the public class name, a .class file is created with no output. That silence means success.
What is the difference between javac and java?
javac is the Java compiler. It reads your .java source file and produces a .class bytecode file. java is the JVM launcher. It reads the .class file and executes the bytecode. You always compile first with javac, then execute with java. They are separate tools with separate jobs.
How do I run Java in VS Code?
Install VS Code, then install the Extension Pack for Java from the Extensions panel. Open your .java file. A Run button appears above the main method automatically. Click it, or press F5 to run with the debugger. VS Code handles compilation and execution in one step, using the JDK installed on your system.
Can I write Java in Notepad?
Yes. Open Notepad, write your Java code, then go to File, Save As, set the file type to All Files, and name the file ClassName.java. If you leave the type as Text Documents, Windows adds a hidden .txt extension and javac won’t find the file. Compile and run from CMD as normal after saving correctly.
Why is my Java program not running in CMD?
The most common reasons are: the JDK PATH is not set correctly, the terminal was not restarted after setting the PATH, the file name does not match the class name, or you included the .class extension when running the java command. Check each of these in order and the problem will almost always resolve.
Last updated: June 2025. Reviewed by the 3University editorial team.


