How to Write Your First Java Hello World Program
Writing your first Java program can be an exciting step in your programming journey. Java, known for its simplicity and versatility, is widely used in everything from mobile applications to large-scale enterprise systems. One of the best ways to begin learning Java is by creating a simple “Hello, World!” program. In this guide, we’ll walk you through the steps to write, compile, and run your first Java program.

Table of Contents
- Prerequisites for Java Programming
- Setting Up Your Development Environment
- Writing Your First Java Program
- Compiling and Running the Program
- Understanding the Code
- Common Errors and Troubleshooting
- Conclusion
1. Prerequisites for Java Programming
Before diving into your first program, you’ll need to ensure you have the necessary tools installed on your computer.
What You Need
- Java Development Kit (JDK): The JDK contains all the tools you need to write, compile, and run Java programs. If you haven’t already installed it, you can download it from the Oracle JDK download page.
- Text Editor or IDE: You can use a simple text editor like Notepad (Windows) or nano (Mac/Linux), but it’s recommended to use an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or NetBeans. IDEs provide useful features such as code completion, error checking, and debugging tools.
Verifying Your Installation
To verify that Java is installed, open a terminal or command prompt and type:
java -version
You should see a message showing the version of Java installed on your system. If you don’t see this message, you may need to reinstall the JDK.
2. Setting Up Your Development Environment
Once you’ve installed Java, set up your development environment by creating a workspace directory where you’ll save your Java programs.
Windows
- Open File Explorer and create a new folder named
JavaPrograms
on your Desktop or in Documents. - This folder will store all your Java projects.
macOS/Linux
- Open a terminal and navigate to your home directory.
- Create a new folder with the command:
mkdir JavaPrograms
Navigate to this folder by typing:
cd JavaPrograms
3. Writing Your First Java Program
Now, let’s create a simple “Hello, World!” program. This program will display a message in the console, introducing you to the basic structure of Java code.
Step-by-Step Guide
- Open your text editor or IDE: If you’re using an IDE, create a new Java project. If you’re using a text editor, simply create a new file named
HelloWorld.java
in theJavaPrograms
directory. - Type the Code: Enter the following code into your editor:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save the File: Make sure the file is saved as HelloWorld.java
. This filename must match the class name (HelloWorld) for the program to compile correctly.
4. Compiling and Running the Program
After writing the code, it’s time to compile and run your first Java program.
Compiling the Program
- Open a terminal or command prompt.
- Navigate to the directory where you saved
HelloWorld.java
:
cd path_to_your_JavaPrograms_directory
3. Use the javac
command to compile the program:
javac HelloWorld.java
- This command will create a file named
HelloWorld.class
in the same directory. This.class
file contains the bytecode, which the Java Virtual Machine (JVM) will execute.
Running the Program
- After compiling, run the program by entering the following command:
java HelloWorld
2. You should see the following output:
Hello, World!
Congratulations! You’ve just written, compiled, and executed your first Java program.
5. Understanding the Code
Let’s break down the code to understand its components:
public class HelloWorld {
- public: This is an access modifier that allows the class to be accessible from other classes.
- class: This keyword defines a new class, which is a blueprint for objects.
- HelloWorld: This is the name of the class. In Java, class names should start with a capital letter, and this name must match the filename.
public static void main(String[] args) {
public: This modifier makes the main
method accessible from outside its class.static: The main
method is static, so it belongs to the class itself rather than an instance of the class.void: This keyword indicates that the main
method does not return any value.main: This is the entry point of any Java application. The JVM looks for the main
method to start execution.String[] args: This is an array of strings that allows the program to accept command-line arguments.
public: This modifier makes the main
method accessible from outside its class.
static: The main
method is static, so it belongs to the class itself rather than an instance of the class.
void: This keyword indicates that the main
method does not return any value.
main: This is the entry point of any Java application. The JVM looks for the main
method to start execution.
String[] args: This is an array of strings that allows the program to accept command-line arguments.
System.out.println("Hello, World!");
System: This is a built-in Java class that provides access to the system, including standard input and output.
out: This is an output stream connected to the console.
println: This method prints a line of text to the console and moves the cursor to the next line.
6. Common Errors and Troubleshooting
You may encounter some common errors when writing and compiling Java code. Here are a few potential issues and how to fix them:
- Error: “javac is not recognized”: This means that the Java compiler is not accessible from the terminal. Ensure that the JDK is installed, and the
PATH
environment variable is correctly set. - Error: “class HelloWorld is public, should be declared in a file named HelloWorld.java”: In Java, the public class name must match the filename. If you’ve named the file incorrectly, rename it to match the class name.
- Error: “Cannot find or load main class HelloWorld”: This can occur if the class file is missing or if you’re in the wrong directory. Make sure you’ve compiled the program in the correct location and are using the correct filename when running the program.
- Syntax Errors: If you miss a semicolon (
;
), curly brace ({}
), or mistype a keyword, Java will display syntax errors when you try to compile. Double-check your code for any typos.
7. Conclusion
Writing your first Java program is a rewarding step towards mastering the language. You’ve learned how to set up your environment, write and compile code, and troubleshoot common errors. With this foundation, you can explore more complex programs, experiment with new ideas, and continue learning Java’s features and capabilities. Keep practicing, and soon, you’ll be developing advanced Java applications!
Other Java Topics
- Introduction to Java Programming Language
- Setting Up Your First Java Development Environment
- Java Syntax
- Java Hello World Program
- Java Data Types: Primitives and Reference
- Java Variables
- Operators in Java
- Java Conditions
- Loops in Java
- Java Arrays
- Object Oriented Programming (OOPs) Concept in Java
- Java Classes and Objects
- Inheritance in Java
- Java Polymorphism
- Encapsulation in Java
- Abstraction in Java
- Java Constructors
- Interfaces in Java
- Abstract Class in Java
- Inner Classes in Java
- Collections in Java
- Java ArrayList
- LinkedList in Java
- HashMap in Java
- Java Sets
- TreeMap in Java
- Comparable vs Comparator in Java