Sunday, January 25, 2026
HomeTech PostHow to run first JAVA program using CMD

How to run first JAVA program using CMD

Here’s a step-by-step guide to run your first Java program using Command Prompt (CMD) 👇


🧩 Step 1: Install Java JDK

  1. Go to the official Oracle Java downloads page.
  2. Download and install the latest JDK (Java Development Kit) for your operating system.
  3. During installation, note the installation path (e.g., C:\Program Files\Java\jdk-21 ```)

⚙️ Step 2: Set Environment Variables (if not already set)

  1. Press Windows + S, search for Environment Variables, and open
    “Edit the system environment variables.”
  2. Click Environment Variables…
  3. Under System Variables, find Path, then click EditNew, and add: C:\Program Files\Java\jdk-21\bin
  4. Click OK on all windows.

✅ To verify installation, open CMD and type:

java -version
javac -version

If you see version numbers, Java is installed correctly.


🧾 Step 3: Write Your First Java Program

  1. Open Notepad (or any text editor).
  2. Type the following code:
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  1. Save the file as HelloWorld.java
    📁 Make sure to save it in a simple folder, e.g.: C:\JavaPrograms\HelloWorld.java

💻 Step 4: Compile the Program

  1. Open Command Prompt.
  2. Navigate to your program folder: cd C:\JavaPrograms
  3. Compile your Java file: javac HelloWorld.java ✅ If successful, this will create a file named HelloWorld.class in the same folder.

▶️ Step 5: Run the Program

Run the compiled program with:

java HelloWorld

💡 Output:

Hello, World!

🧠 Common Tips:

  • File name must match the class name (case-sensitive).
  • Don’t include .java when running with java command.
  • If you get “javac is not recognized”, recheck your PATH settings.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments