First Java Program – Compile and Run HelloWorld

⏱️ 7 min read • Beginner Level • Lesson 3

Lesson 3 of Java Tutorial

In this lesson, you will learn how to write, compile, and run your first Java program. We will create a simple HelloWorld.java file, compile it using the javac command, and execute it using the java command.

Before starting this lesson, make sure you have completed Java environment setup. If you are new to Java, you can also read Java introduction.


What You Need Before Writing Java Program

  • You can write Java code using any text editor like Notepad, Notepad++, VS Code, or gedit.

  • You can also use Java IDEs such as BlueJ, NetBeans, Eclipse, or IntelliJ IDEA.

  • To compile and run Java programs, your system must have JDK installed and configured.

Steps to Write and Run a Java Program

1. Create the Source Code

Write your Java program in a text editor and save the file with the .java extension.

In this example, we will create a file named HelloWorld.java.

Creating HelloWorld Java source file

Example: HelloWorld.java

The following program prints Hello World on the screen:

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

2. Compile the Program

Open Command Prompt or terminal and go to the folder where your HelloWorld.java file is saved.

For example, if the file is saved on Desktop, use:

Change Directory
cd Desktop
Open command prompt to compile Java program

Now compile the Java source file using:

Compile Command
javac HelloWorld.java

After successful compilation, a HelloWorld.class file is generated.

Java compilation process creating class file

3. Run the Java Program

After compilation, run the program using the class name:

Run Command
java HelloWorld
Note: Do not include .class in the execution command. Use java HelloWorld, not java HelloWorld.class.
Run HelloWorld Java program

Explanation of HelloWorld Program

  • public: access modifier that makes the class or method accessible from anywhere.
  • class: keyword used to declare a class.
  • HelloWorld: name of the class. The file name should be HelloWorld.java.
  • static: allows the main method to run without creating an object.
  • void: means the method does not return any value.
  • main: entry point of the Java program.
  • String[] args: stores command-line arguments.
  • System.out.println(): prints output on the console.

Source Code Facts

  • Java source code is written by the programmer.
  • Java source files are saved with the .java extension.
  • A Java source file may contain multiple classes.
  • If a Java source file contains a public class, the file name must match the public class name.
Java Source File Structure
package declaration;
import statements;

class ClassName {
    // class members
}   

Bytecode Facts

  • The Java compiler generates bytecode after successful compilation.
  • Bytecode is stored in a .class file.
  • Bytecode is platform-independent.
  • JVM interprets and runs bytecode.
  • Each compiled Java class generates its own bytecode file.
Java bytecode generation process
Summary:
  • A Java source file is saved with the .java extension.
  • The javac command compiles Java source code.
  • Compilation creates a .class bytecode file.
  • The java command executes the compiled class using JVM.
  • The class name should match the file name if the class is public.

Next step: Understand JDK, JRE, and JVM.

🚀 Continue to JDK, JRE, and JVM →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Writing_executing_java | Language: Java

Q1. Which tool is responsible for interpreting and running Java bytecode?
Q2. What is the purpose of 'System.out.println()' in Java?
Q3. Which file extension must be used when saving a Java source code file?
Q4. Which command is used to execute a compiled Java program?
Q5. What is Java bytecode?
Q6. Which statement about bytecode is correct?
Q7. After compiling HelloWorld.java successfully, which file will be generated?
Q8. Which command is used to compile a Java program?
Q9. In Java, which method serves as the entry point of execution?
Q10. Which of the following is NOT true about Java source code?

🎉 Great job! Continue learning Java step by step.