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 (CMD, Notepad, VS Code)

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

    To run a Java program: install the JDK, write your code, save the file as ClassName.java, open Command Prompt, type javac ClassName.java to compile, then type java ClassName to run. The entire process takes under five minutes once your PATH environment variable is correctly set.

    • Key Takeaway 1: The JDK (Java Development Kit) includes the compiler (javac) and the runtime (java). You need it installed before anything else works.
    • Key Takeaway 2: Your file name must match your public class name exactly, including capitalisation. HelloWorld.java must contain public class HelloWorld.
    • Key Takeaway 3: The PATH environment variable tells Windows where to find javac and java. Getting this wrong is the single most common beginner mistake.
    • Key Takeaway 4: VS Code with the Extension Pack for Java gives you IntelliSense, inline errors and one-click run, making it the fastest modern setup for students.
    • Key Takeaway 5: Java compiles to bytecode, not machine code. The JVM (Java Virtual Machine) then interprets that bytecode, which is why the same .class file runs on Windows, Linux and macOS without recompiling.

    Install the JDK and Set the PATH Variable

    The JDK (Java Development Kit) is the foundation for learning how to run a Java program. It bundles the compiler (javac), the Java Runtime Environment (JRE), and the JVM into one installer. Without it, your system has no idea what to do with a .java file. According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, used by roughly 30% of professional developers, so setting this up correctly is a skill worth getting right.

    Head to oracle.com/java and download the latest JDK for Windows (the LTS version, currently JDK 21, is the safest choice for students). Run the installer and note the installation path, typically something like C:\Program Files\Java\jdk-21.

    Setting the PATH on Windows

    After installing, open System Properties, go to Advanced, click Environment Variables, find the Path variable under System Variables, and add the path to the JDK’s bin folder, for example C:\Program Files\Java\jdk-21\bin. Click OK on every dialog. Then open a fresh Command Prompt window and type java -version. You should see something like java version "21.0.3".

    If you see ‘java’ is not recognized as an internal or external command, the PATH is not set. Double-check the folder path and make sure you opened a new CMD window after saving the variable. Old CMD sessions do not pick up PATH changes.

    How to Fix the javac Not Recognized Error in CMD

    This is the most common error beginners hit when trying to run a Java program for the first time. The fix is always the same: confirm that the bin folder inside your JDK installation is listed in the System PATH variable, not the User PATH. Then close every open CMD window and open a fresh one. Type javac -version to confirm the fix worked. If you still see the error, check that the path you added does not have a trailing backslash and that the JDK folder actually exists at that location.

    Understanding JDK, JRE and JVM

    These three terms confuse almost every beginner. Here is the short version: the JDK is what you install to develop Java programs. The JRE is the runtime environment that end users need to run compiled Java apps. The JVM is the engine inside the JRE that reads bytecode and executes it on the actual hardware. When you compile a .java file, javac produces a .class file full of bytecode. The JVM reads that bytecode at runtime.

    Component Full Name Role Needed By
    JDK Java Development Kit Compile and develop Java programs Developers
    JRE Java Runtime Environment Run compiled Java applications End users
    JVM Java Virtual Machine Execute bytecode on the host OS Bundled inside JRE
    javac Java Compiler Convert .java source to .class bytecode Bundled inside JDK
    java Java Launcher Launch the JVM and run a .class file Bundled inside JDK/JRE

    How to Run a Java Program in Notepad and CMD

    Yes, you absolutely can write and run a Java program using only Notepad and Command Prompt. It is a legitimate approach and many Indian engineering colleges, including those affiliated with VTU, SPPU and Anna University, still teach this method first because it forces you to understand the compile-run cycle without a safety net. The trick is in how you save the file.

    Writing Your First Java Program in Notepad

    Open Notepad and type the following exactly:

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

    Now go to File, Save As. In the “Save as type” dropdown, choose All Files (not Text Documents). Name the file HelloWorld.java. This step trips up a huge number of beginners: if you leave it as “Text Documents”, Windows silently saves it as HelloWorld.java.txt, and javac will not find it.

    Compiling and Running from CMD

    Open Command Prompt. Use cd to navigate to the folder where you saved the file. For example, if you saved it on the Desktop: cd C:\Users\YourName\Desktop. Then compile with:

    javac HelloWorld.java

    If there are no errors, the command prompt returns to a blank line. That is success. A new file called HelloWorld.class now exists in the same folder. Run the Java program with:

    java HelloWorld

    Notice you do not type java HelloWorld.class. Drop the extension. The JVM looks for a class named HelloWorld, not a file named HelloWorld.class. Getting this wrong throws a ClassNotFoundException.

    Why the Class Name Must Match the File Name

    Java enforces a strict rule: a public class must be declared in a file with the exact same name, including capitalisation. helloworld.java with public class HelloWorld inside it will fail to compile. The error message looks like: “class HelloWorld is public, should be declared in a file named HelloWorld.java”. There is no way around this rule, so get the habit right from day one.

    Common Errors and Quick Fixes When Running a Java Program

    • ‘javac’ is not recognized: Your PATH variable does not point to the JDK bin folder. Fix the environment variable and open a new CMD window.
    • ClassNotFoundException: You typed java HelloWorld.class instead of java HelloWorld. Remove the .class extension.
    • Main method not found: Your main method signature is wrong. It must be exactly public static void main(String[] args). Check for typos like Static with a capital S or a missing void.
    • File saved as .txt: In Notepad’s Save As dialog, switch “Save as type” to “All Files” before saving.
    • Compilation errors with semicolons: Java is case-sensitive and every statement ends with a semicolon. Missing one causes a compile error on the next line, which can be confusing.

    According to TIOBE Index data from June 2025, Java consistently ranks in the top three programming languages worldwide, and it remains the primary language for Android development and backend enterprise systems. Students in Indian IT programmes at institutions like IITs, NITs and private engineering colleges frequently encounter Java as their first compiled language, making this exact compile-run workflow a foundational skill.

    How to Run a Java Program in VS Code, IntelliJ and Eclipse

    Once you have run a Java program using the Notepad-and-CMD approach a few times, you understand what is happening under the hood. VS Code automates those steps without hiding them from you, which makes it the best of both worlds for learners.

    Setting Up VS Code for Java

    Download VS Code from code.visualstudio.com. Open the Extensions panel (Ctrl+Shift+X) and search for Extension Pack for Java by Microsoft. Install it. This single pack includes the Language Support for Java, Debugger for Java, Maven for Java, and Test Runner, giving you everything for a complete Java workflow.

    VS Code detects your installed JDK automatically if the PATH is already set. If it does not, press Ctrl+Shift+P, type Java: Configure Java Runtime, and point it to your JDK folder manually.

    Running a Java Program in VS Code Step by Step

    1. Open VS Code and create a new file called HelloWorld.java.
    2. Type or paste your Java code. VS Code will immediately show red underlines for syntax errors as you type.
    3. Click the Run button (the play icon at the top right) or press F5.
    4. The integrated terminal opens and shows the output: Hello, World!

    Behind the scenes, VS Code is running javac and java for you. You can see the exact commands it uses in the terminal output if you look closely. Understanding that is what separates developers who can debug broken build setups from those who cannot.

    VS Code vs IntelliJ IDEA vs Eclipse

    For pure beginners, VS Code is the lightest option and the fastest to set up. IntelliJ IDEA (Community Edition is free) is the industry standard for professional Java development and is widely used in Indian IT companies and startups. Eclipse is older but still common in academic settings. According to the JetBrains Developer Ecosystem Survey 2024, IntelliJ IDEA is used by over 60% of Java developers professionally, while VS Code is the preferred editor for those working across multiple languages.

    If you are a student exploring multiple languages alongside Java, VS Code makes sense. If Java is going to be your primary language professionally, invest time in IntelliJ. Both tools run the same javac and java commands underneath.

    Learning to compile and run a Java program properly is your entry point into software development. Once this clicks, you will find that the same logical structure, write, compile, debug, run, applies to almost every compiled language you pick up later. If you are thinking about where Java fits in a broader tech career, the best career paths after 12th grade in 2026 guide covers how programming skills map to actual job roles in India’s tech industry. Java is also central to Android development and enterprise backend work, and it appears frequently in cybersecurity tooling, which is one reason cybersecurity project guides for students often assume Java fluency.

    According to NASSCOM’s Indian Tech Talent Report 2024, over 5 million software professionals in India work with Java in some capacity, making it the single most commercially relevant language for campus placements and entry-level developer roles. Getting the basics right now pays off directly in interviews and internships.

    If you want to go further with programming and explore how languages like Java, Python and Solidity are used in cutting-edge fields, check out the top programming languages for blockchain developers in 2026. And if you are a student looking for free tools and resources to support your learning, the GitHub Education Program updates are worth reading, since the GitHub Student Pack gives you free access to tools that professional Java developers use daily.

    The practical next step is simple: install the JDK today, write a ten-line Java program in Notepad, compile it in CMD, and run it. That single experience builds more confidence than reading five tutorials. Then move to VS Code, explore the debugger, and start building something small but real. Explore the full range of online certification courses at 3.0 University to take your skills from beginner to industry-ready, with structured programmes in Cybersecurity, Ethical Hacking, AI, Blockchain and Web3. The Java and tech learning guides at 3.0 University also cover free resources across every major tech topic if you want to keep building knowledge before committing to a course.

    Frequently Asked Questions

    How do I run a Java program in CMD?

    Open Command Prompt, navigate to the folder containing your .java file using cd FolderPath, compile with javac HelloWorld.java, then run with java HelloWorld. Make sure the JDK is installed and its bin folder is added to your PATH environment variable. Without the correct PATH, CMD will not recognise javac or java as commands.

    How do I compile a Java program using javac?

    Type javac Filename.java in Command Prompt from the directory where your file is saved. The compiler checks your code for syntax errors and, if none exist, produces a Filename.class bytecode file. If you see errors, read the line numbers in the output, fix the issues in your editor, save, and recompile. The process is always edit, save, compile, run.

    Can I write Java in Notepad?

    Yes. Open Notepad, write your Java code, and go to File, Save As. Set “Save as type” to All Files and name the file exactly as your public class name with a .java extension, for example HelloWorld.java. If you skip the “All Files” step, Windows saves it as HelloWorld.java.txt, which the compiler will not find. Notepad works fine for learning the basics.

    How do I run a Java program in VS Code?

    Install VS Code, then add the Extension Pack for Java from the Extensions marketplace. Open or create a .java file, write your code, and click the Run button at the top right or press F5. VS Code compiles and runs the program automatically in its integrated terminal. Make sure the JDK is installed first, as VS Code relies on it behind the scenes.

    Why must my Java class name match the file name?

    Java requires that a public class be stored in a file with the identical name, including capitalisation. If your class is public class StudentMarks, the file must be StudentMarks.java. The compiler enforces this rule and throws an error if there is a mismatch. It is a language design decision that makes it straightforward for the JVM to locate class definitions in large projects.

    What is the difference between JDK and JRE?

    The JDK (Java Development Kit) is the full package you install to write and compile Java programs. It includes the compiler (javac), the JRE, and development tools. The JRE (Java Runtime Environment) is a smaller package that only lets you run already-compiled Java programs. As a developer learning how to run a Java program from scratch, you always install the JDK, not just the JRE.

    Can I run a Java program without installing the JDK?

    No. To compile and run a Java program on your own machine, you must install the JDK. There is no way around this for local development. However, you can use browser-based Java compilers like JDoodle or Replit to write and run Java code online without any local installation, which is useful for quick experiments before you set up your full environment.

    Last updated: June 2025. Reviewed by the 3University editorial team.

    • Share:
    3.0 University

    Previous post

    How to Write, Compile and Run a C Program (Step by Step)
    August 16, 2026

    Next post

    How to Write and Run a Python Program (Beginner Guide)
    August 16, 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