Here’s a step-by-step guide to run your first Java program using Command Prompt (CMD) 👇
🧩 Step 1: Install Java JDK
- Go to the official Oracle Java downloads page.
- Download and install the latest JDK (Java Development Kit) for your operating system.
- During installation, note the installation path (e.g.,
C:\Program Files\Java\jdk-21 ```)
⚙️ Step 2: Set Environment Variables (if not already set)
- Press Windows + S, search for Environment Variables, and open
“Edit the system environment variables.” - Click Environment Variables…
- Under System Variables, find Path, then click Edit → New, and add:
C:\Program Files\Java\jdk-21\bin - 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
- Open Notepad (or any text editor).
- Type the following code:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- 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
- Open Command Prompt.
- Navigate to your program folder:
cd C:\JavaPrograms - 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
.javawhen running withjavacommand. - If you get
“javac is not recognized”, recheck your PATH settings.
0 Comments