👋 Java Hello World: Your First Program Guide

This guide covers how to write, compile, and run your first Java program — "Hello World" — using:

  • ✅ Command Line (macOS/Linux/Windows)
  • 🌀 Eclipse IDE
  • 🧠 IntelliJ IDEA
  • 💻 Visual Studio Code

💻 Understanding the Java Hello World Program

Let's break down the classic "Hello World" program in Java:

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

📝 Code Explanation

  • public class HelloWorld: Defines a class named HelloWorld that can be accessed from anywhere
  • public static void main(String[] args): The entry point method that Java looks for when running a program
    • public: Accessible from anywhere
    • static: Belongs to the class, not an instance
    • void: Returns no value
    • main: Special method name that Java recognizes as the starting point
    • String[] args: Command-line arguments passed to the program
  • System.out.println("Hello, World!");: Prints the text "Hello, World!" to the console
    • System: A built-in class that provides access to the system
    • out: The standard output stream
    • println(): A method that prints text and adds a new line

🔧 Running Java Hello World from the Command Line

📝 Step 1: Save Your File

Save the code as HelloWorld.java. Important: The filename must match the class name exactly, including capitalization.

💡 Step 2: Compile the Program

Open Terminal (macOS/Linux) or Command Prompt (Windows) and navigate to the directory containing your file:

javac HelloWorld.java

This command invokes the Java compiler (javac), which converts your human-readable code into bytecode that the Java Virtual Machine (JVM) can understand. If successful, it creates a HelloWorld.class file.

▶️ Step 3: Run the Program

java HelloWorld

This command starts the JVM and executes your program. The output should be:

Hello, World!

⚠️ Common mistakes:

  • Do not include .class or .java when running with the java command
  • Make sure you're in the correct directory
  • The class name and filename must match exactly (case-sensitive)

🔍 Troubleshooting Java Command Line Issues

Error Possible Cause Solution
'javac' is not recognized... Java not installed or not in PATH Set up your Java environment
Class not found: HelloWorld Wrong directory or filename Check file location and name
Public class HelloWorld must be in file HelloWorld.java Filename doesn't match class name Rename file to match class name

🌀 Running Java in Eclipse IDE

🔧 Step 1: Create a Java Project

  • Open Eclipse
  • Go to FileNewJava Project
  • Enter project name (e.g., HelloWorldProject)
  • Click Finish

📦 Step 2: Add a Java Class

  • Right-click on the src folder in the Project Explorer
  • Select NewClass
  • Enter HelloWorld as the class name
  • Check the box "public static void main(String[] args)"
  • Click Finish

✍️ Step 3: Enter Code

The editor will open with a template. Replace or modify the generated code so it looks like:

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

▶️ Step 4: Run

  • Right-click the file in the Project Explorer → Run AsJava Application
  • Alternatively, click the Run button (▶️) in the toolbar
  • Output appears in the Console view at the bottom of the screen

🔍 Eclipse Tips

  • Auto-format code: Press Ctrl+Shift+F (Windows/Linux) or Cmd+Shift+F (macOS)
  • Quick fix errors: Hover over red underlines and press Ctrl+1 for suggestions
  • Content assist: Press Ctrl+Space for code completion suggestions

🧠 Running Java in IntelliJ IDEA

🔧 Step 1: Create New Project

  • Open IntelliJ IDEA
  • Click New Project
  • Choose Java from the left panel
  • Select your JDK from the dropdown (or set up a new one)
  • Click Next
  • Set project name (e.g., HelloWorldApp)
  • Click Finish

📂 Step 2: Add Java Class

  • Right-click the src folder in the Project view → NewJava Class
  • Name it HelloWorld

✍️ Step 3: Enter Code

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

▶️ Step 4: Run

  • Click the green run triangle (▶️) in the gutter next to the main method
  • Alternatively, right-click anywhere in the editor → Run 'HelloWorld.main()'
  • Output shows in the Run tool window at the bottom

🔍 IntelliJ IDEA Tips

  • Auto-format code: Press Ctrl+Alt+L (Windows/Linux) or Cmd+Option+L (macOS)
  • Quick documentation: Press Ctrl+Q (Windows/Linux) or F1 (macOS) to view documentation
  • Smart code completion: Press Ctrl+Shift+Space for context-aware suggestions

💻 Running Java in Visual Studio Code

🔧 Step 1: Install Extensions

  • Open VS Code
  • Go to Extensions view (click the square icon on the sidebar or press Ctrl+Shift+X)
  • Search for "Extension Pack for Java" and install it

📂 Step 2: Create a Java Project

  • Press Ctrl+Shift+P to open the Command Palette
  • Type "Java: Create Java Project" and select it
  • Choose "No build tools" for a simple project
  • Select a location for your project
  • Enter a project name (e.g., HelloWorldVSCode)

✍️ Step 3: Create a Java Class

  • In the Explorer view, expand your project folder
  • Right-click on the src folder → New File
  • Name it HelloWorld.java
  • Enter the Hello World code:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

▶️ Step 4: Run

  • Click the "Run" link that appears above the main method
  • Alternatively, right-click in the editor → Run Java
  • Output appears in the "Terminal" panel at the bottom

🔍 VS Code Java Tips

  • Code actions: Hover over code and click the light bulb 💡 for quick fixes
  • Debugging: Set breakpoints by clicking in the gutter and press F5 to debug
  • Java Projects view: Click the Java Projects icon in the sidebar for a structured view

🎓 Summary

Method Tools/Commands Key Advantages
Terminal javac HelloWorld.javajava HelloWorld No additional software needed
Eclipse Create Project → Add Class → Run as Application Comprehensive IDE with many plugins
IntelliJ Create Project → Add Java Class → Run Intelligent features, modern interface
VS Code Install Extensions → Create Project → Run Java Lightweight, customizable editor

🔍 Common Errors and Solutions

Error Message Cause Solution
error: class HelloWorld is public, should be declared in a file named HelloWorld.java Filename doesn't match class name Rename file to match class name exactly
error: cannot find symbol Using a variable or method that hasn't been declared Check variable names and imports
error: ';' expected Missing semicolon at the end of a statement Add the missing semicolon
error: reached end of file while parsing Missing closing brace } Check that all opening braces have matching closing braces

🎯 Next Steps

After mastering the Hello World program, you're ready to:

  1. Learn about Java variables and data types
  2. Explore control flow statements like if-else and loops
  3. Practice with more complex programs that use methods

Happy coding! 🎉

Hello World in Java: First Program Guide for Beginners | Stack a Byte