Hello, World!

It’s time to write your first Java program! Here is a step-by-step guide on how to write your program in different ways.

 

Option 1: Using IntelliJ IDEA

Create a New Project

  • On the welcome screen, click New Project.
  • In the new project window:
    • Select Java from the project templates.
    • Make sure your JDK is selected (If not, click Add JDK and locate the JDK installation path).
    • Click Next.

Configure the Project

  • Project Name: Enter a name for your project (e.g., HelloWorld).
  • Project Location: Choose a folder where the project will be saved.
  • Click Finish.

Create a New Java Class

  • In the Project window on the left, expand the src directory.
  • Right-click on src, select New > Java Class.
  • In the pop-up window, name the class (e.g., HelloWorld) and click OK.

Write Your Java Code

  • IntelliJ will open the newly created class file.
  • Write the following code in the HelloWorld.java file:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Run the Program

  • To run your program, click the Run button (a green triangle) in the top toolbar or right-click the class file and select Run ‘HelloWorld’.
  • You should see Hello, World! printed in the console at the bottom of the screen.

 

Option 2: Using Eclipse

Create a New Java Project

  • In the Eclipse main window, go to File > New > Java Project.
  • In the new project window:
    • Project Name: Enter the name of your project (e.g., HelloWorld).
    • Click Finish to create the project.

Create a New Java Class

  • In the Project Explorer on the left, expand your project folder.
  • Right-click the src folder, then select New > Class.
  • In the new class window:
    • Name: Enter the class name (e.g., HelloWorld).
    • Check the box to include the public static void main(String[] args) method.
    • Click Finish.

Write Your Java Code

  • The newly created class will open in the editor.
  • If the main method is not automatically generated, write the following code:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Run the Program

  • Click the Run button (green circle with a white triangle) in the toolbar or right-click on the class file and select Run As > Java Application.
  • The console at the bottom will display Hello, World!.

 

Option 3: Terminal / Command Prompt

Open Terminal or Command Prompt

  • Windows: Open Command Prompt (type cmd in the Start menu).
  • macOS/Linux: Open the Terminal application.

Create a Java File

  • Navigate to the directory where you want to save your Java file. For example, you can create a folder named java_projects:
mkdir java_projects
cd java_projects
  • Use a text editor to create the Java file. For example, you can use nano (Linux/macOS) or notepad (Windows):
Linux/macOS
nano HelloWorld.java
Windows
notepad HelloWorld.java
  • In the text editor, write the following code:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Save the file and exit the editor:

  • nano: Press CTRL + X, then Y to confirm and Enter to save.
  • notepad: Simply save and close the file.

Compile the Java Program

  • In the terminal, compile the Java program using the javac command:
javac HelloWorld.java
  • If the compilation is successful, no output will appear, but a HelloWorld.class file will be generated in the directory.

Run the Java Program

  • Run the compiled program using the java command:
java HelloWorld
  • You should see the following output in the terminal:
Hello, World!
Last updated on