3.0 University logo
  • Home
  • About us
  • All Courses
    • Cybersecurity Programs
      • Certified Ethical Hacker (CEH v13)
      • Certified SOC Analyst
      • Certified Penitration Testing Professional
      • Computer Hacking Forensic Investigator
      • Certified Cybersecurity Technician (CCT)
      • Certified AI Program Manager
      • Certified Offensive AI Security Professional
      • Certified Responsible AI Governance & Ethics Professional
      • Artificial Intelligence Essentials
    • Crypto Market Programs
    • Blockchain & Web3 Programs
      • Digital Assets Trading & Analysis Program
      • Certified Web3 Strategy & Growth Specialist
      • Certified Web3 Governance & Compliance Expert
      • Full Stack Blockchain Developer Program
      • Private Blockchain Developer Program
      • Public Blockchain Developer Program
    • IGM x IIG Programs
      • Jewellery Design Executive Program
      • Gems & Diamond Specialist Program
      • Jewellery Business Specialist Program
  • Schools
    • School of Decentralized Economics
    • School of Cyber Resilience
    • School of Intelligent Systems
    • School of Design Thinking
  • Partners
    • Certification & Knowledge Partner
    • Academic Partner
    • Hiring Partner
    • Delivery Partner
    • Affiliate Partner
    • Hybrid Center Partner
  • Blog
  • Home
  • About us
  • All Courses
    • Cybersecurity Programs
      • Certified Ethical Hacker (CEH v13)
      • Certified SOC Analyst
      • Certified Penitration Testing Professional
      • Computer Hacking Forensic Investigator
      • Certified Cybersecurity Technician (CCT)
      • Certified AI Program Manager
      • Certified Offensive AI Security Professional
      • Certified Responsible AI Governance & Ethics Professional
      • Artificial Intelligence Essentials
    • Crypto Market Programs
    • Blockchain & Web3 Programs
      • Digital Assets Trading & Analysis Program
      • Certified Web3 Strategy & Growth Specialist
      • Certified Web3 Governance & Compliance Expert
      • Full Stack Blockchain Developer Program
      • Private Blockchain Developer Program
      • Public Blockchain Developer Program
    • IGM x IIG Programs
      • Jewellery Design Executive Program
      • Gems & Diamond Specialist Program
      • Jewellery Business Specialist Program
  • Schools
    • School of Decentralized Economics
    • School of Cyber Resilience
    • School of Intelligent Systems
    • School of Design Thinking
  • Partners
    • Certification & Knowledge Partner
    • Academic Partner
    • Hiring Partner
    • Delivery Partner
    • Affiliate Partner
    • Hybrid Center Partner
  • Blog
    Login
    ₹0.00 0 Cart

    Learn Articles

    • Home
    • Learn Articles

    How to Compile and Run a Java Program in CMD, VS Code and Notepad

    • Posted by 3.0 University
    • Date August 31, 2026
    • Comments 0 comment

    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: javac compiles source code into bytecode; java runs 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.

    • Share:
    3.0 University

    Previous post

    What Is Java Programming? Features, Uses and Why It Still Endures
    August 31, 2026

    Next post

    What Is R Programming? Uses, Learning Curve and R vs Python
    August 31, 2026

    You may also like

    Free AI Certificate Course by Government of India
    FREE AI Course with Certificate Launched by Govt of India
    June 19, 2026
    Highest Paid Professions in India
    Highest Paid Profession in India
    June 12, 2026
    Cyber Security Course Eligibility
    Cyber Security Course Eligibility
    June 11, 2026

    Leave A Reply Cancel reply

    You must be logged in to post a comment.

    3.0 University is a pioneering academic initiative for creating a comprehensive knowledge ecosystem for emerging technologies. We have developed an in-house suite of course offerings for retail, institutional market participants and industry-at-large. 

    Facebook X-twitter Instagram Linkedin
    Quick Links
    • About us
    • Courses
    • Become a Partner
    • Contact Us
    • Blog
    • Learn
    Trending Courses
    • Certified SOC Analyst
    • Certified Ethical Hacker v13 Program
    • Certified Penitration Testing Professional
    • Full Stack Blockchain Developer
    • Certified AI Program Manager
    Policies
    • Privacy Policy
    • Terms and Conditions
    • Disclaimer
    • Refund Policy
    Contact Us
    FT Tower, CTS No. 256 & 257, Suren Road, Chakala, Andheri (E), Mumbai-400093 India.

    +91 8657961141

    support@3university.io

    Login with your site account

    Lost your password?

    Not a member yet? Register now

    Register a new account

    Are you a member? Login now

    Login with your site account

    Lost your password?

    Not a member yet? Register now

    Register a new account

    Are you a member? Login now

    Sign In

    Welcome back! Or create an account

    OR
    Forgot password?

    Need a new verification email?

    Don't have an account? Register

    Create Account

    Already have an account? Sign in

    OR

    Already have an account? Log in

    Reset Password

    Enter your email and we'll send you a reset link.

    ← Back to login

    Check Your Email

    Almost there!
    We have sent a verification link to your email address. Please check your inbox (and spam folder) and click the link to activate your account.

    Didn't receive the email? Enter your address to resend:

    Already verified? Sign in