Developing Java Hello World Program

Summary: in this tutorial, you will learn how to develop the first simple but famous program called Java Hello World using both Eclipse IDE and JDK tools.

If you ask any experience Java developer about his or her first program, the answer you will get is “Hello World”. The reason is that the Java Hello World program is so simple to develop and more importantly it helps you get started with Java programming more quickly.

We will show you how to develop the Java Hello World program that will display a message “Hello World” in a console window.

Java Hello World Program with Eclipse IDE

If you don’t have JDK or Eclipse installed, check it out the setting up Java development environment tutorial.

First, launch the Eclipse IDE.

Next, from the File menu choose New > Java project.

Eclipse - New Java Project

Then, enter the project name: HelloWorld and click the Next button. If you have multiple versions of Java Runtime Environment or JRE installed, you can choose the JRE that your project is targeting.

Eclipse - Enter Java Project

Eclipse builds the project based on the settings you make in the Java Settings window that is shown as below; just accept the default and click the Finish button.

Eclipse - Java Build Settings

After that, from the src icon on the Package Explorer pane on the left side of the Eclipse IDE, right click to show the context menu and choose the menu item New > Class.

Eclipse - New Java Class

Finally, enter the information for the new class as the following screenshot and click the Finish button.

Eclipse - Hello Java Class

Inside the workspace folder c:\javaws, Eclipse creates a new folder for the project called HelloWorld. Inside the HelloWorld project folder, there are two sub-folders: src for storing Java source code files and bin for storing the class files.

Java Hello World Project Folder

Because we entered the package for the project as com.zentut so Eclipse created a new subfolder named zentut inside the folder com. In Java, a package name is mapped to physical folder structure. We will discuss more about the Java package in the next section.

We asked Eclipse create a new class named Hello inside the package com.zentut therefore Eclipse created a new file named inside the folder zentut.

Eclipse - Hello Java Class Source Code

We have everything is place. Let’s change the code of the file to the following code:

package com.zentut; public class Hello { /** * @param args */ public static void main(String[] args) { System.out.println("Hello World"); } }
Code language: PHP (php)

To execute the Java Hello World program, from the Run menu or Run button on the toolbar, choose Run As > Java Application:

Eclipse - Run the Hello World Program

If everything is correctly, you will see the output of the program in the console window of the Eclipse IDE as below:

Java Hello World Output

Congratulation! You’ve got your first Java program up and running.

Let’s examine the program in a greater detail.

Java Helo World Code Detail

At the top of the file, we declared a new package named com.zentut. Java uses packages to organize its classes into namespaces. In other words, a package contains related Java classes.

Next we defined a new class Hello with the keyword class. Inside the class Hello, we have a special method called main(), which is the entry-point method for the program.

The main() method has a header and body.

The main() method header:

  • public means that the main() method is visible to the start-up code that calls the method.
  • static associates the method with the class instead of object. We use static because the start-up code does not create object from the class to call the main() method.
  • void means that the main() method does not return any value.
  • String[] args is a parameter that accepts a list of arguments passing from the command-line arguments

The main() method body:

  • Inside the main() method body, we called the method println() of the object System.out to output a string to standard output.

Above the main() method header, we have comments between character /* and */, which are used to document the code (more about Java comment in Java syntax tutorial).

Java Hello World with JDK Tools

We will show you to develop, compile and execute the Java Hello World program in JDK command-line tools to help you understand what Eclipse does behind the scenes.

Creating Java Hello World program folder structure:

  • First, create a new folder named HelloWorld2 in any folder in you hard-drive e.g., c:\javaws
  • Second, under the folder HelloWorld2, create the following folders: com and com\zentut
  • Third, create a new file named inside the folder zentut and enter the Java code above.
Java Hello World Folder Structure

Compiling and executing Java Hello World from command-line interface

First we need to compile the Java source code into Java bytecode using utility javac that comes with JDK. We use the following command to compile the file

c:\>javac c:\javaws\HelloWorld2\com\zentut\
Code language: CSS (css)

If you take a look at the folder C:\javaws\HelloWorld2\com\zentut, you will see that a new file Hello.class has just been created.

Java class file generated

Second to run the class file we use the java tool as follows:

c:\>java -cp c:\javaws\HelloWorld2 com.zentut.Hello
Code language: CSS (css)

There are three parts in the command:

  • First, java is used to launch java.exe tool.
  • Second, -cp c:\javaws\HelloWorld2 is the command line option of the java tool called CLASSPATH. It instructs the java tool search for Java classes in the folder c:\javaws\HelloWorld2 during classes loading.
  • Third, we put class name Hello that precedes with the package name com.zentut at the end to launch the Java Hello World program.

In this tutorial, we’ve shown you how to develop the Java Hello World program using both Eclipse IDE and JDK tools.