Java "Hello World" Program: Step-by-Step Guide


Step 1: Set Up Your Environment
1.    Install JDK: Download and install the Java Development Kit (JDK) from Oracle's official website.
2.    Set Path:
•    On Windows, configure the PATH variable to include the bin directory of the JDK.
•    On Linux/Mac, you can add the path to your shell configuration file (e.g., .bashrc or .zshrc).
 
Step 2: Write Your First Java Program
1.    Open a text editor or an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or NetBeans.
2.    Create a new file named HelloWorld.java.
3.    Add the following code:

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

 

Explanation of the Code
1.    public class HelloWorld: Defines a class named HelloWorld. In Java, every application must have at least one class.
2.    public static void main(String[] args): The main method is the entry point for any Java program.
3.    System.out.println("Hello, World!");: This line prints the text "Hello, World!" to the console
 


Step 3: Compile the Program
1.    Open a terminal or command prompt.
2.    Navigate to the directory where your HelloWorld.java file is saved.
3.    Compile the program using the javac command; if you are using an IDE just click run and compile button.

 

javac HelloWorld.java


 

Step 4: Run the Program
1.    Execute the compiled program using the java command:

java HelloWorld


2.    You should see the output:

              Hello, World!

 

Code Example
public class helloworld {
    public static void main(String[] args){
       System.out.println("Hello, World!");
    }
}
Code Playground
Java
public class helloworld {

    }
}
Practice Exercise
public class helloworld {     public static void main(String[] args){        System.out._______("Hello, World!");     } }
Lesson Navigation
Previous: What is JAVA