Step 3: Run the Java Program
Now that the program is compiled, we can run it!
- In CMD, type:
java MyFirstProgram
2. Press Enter.
If everything is correct, you will see this output:
Hello, Java!
Click here for an explanation on java
and javac
commands
The javac
command is used to compile your Java source file (.java
) into a bytecode file (.class
). For example, if your file is named MyFirstProgram.java
, you would use the command javac MyFirstProgram.java
to compile it. If there are any errors in your code, the compiler will display them, and no bytecode will be created. Once the code is error-free, the java
command is used to run the compiled program. For example, you would type java MyFirstProgram
to execute the program, without adding .class
at the end. This will start the program and display the output on your screen.
Summary:
🔹 Create a Folder for Your Project (e.g., JavaProjects → FirstProgram) │ ▼ 🔹 Open Notepad and Write Code (Save as MyFirstProgram.java in FirstProgram folder) │ ▼ 🔹 Open CMD in the Folder (Go to FirstProgram folder, click in the address bar, type 'cmd' and press Enter) │ ▼ 🔹 Compile the Java Program Command: javac MyFirstProgram.java │ ▼ 🔹 If No Errors, Bytecode is Created (MyFirstProgram.class) │ ▼ 🔹 Run the Program Command: java MyFirstProgram │ ▼ ✅ Output Appears on Screen: "Hello, Java!"