Writing Our First Java Program

Now that we are all set with Eclipse and Java, we will write our first Java program and run it.

Step 1

Launch Eclipse from the shortcut created on the Desktop or from the Start menu. Select the workspace when prompted.

Step 2

On the Eclipse homepage, click on Create a new Java project button.

click on Create a new Java project

Step 3

Next, in the New Java Project screen, enter the Java project’s name. For example, ‘Hello World‘ and click on the Finish button.

enter project name and click Finish button

Step 4

Our new Java project is now open. In the Package Explorer section on the left side, click-open the Hello World project folder and create a new Package under the ‘src’ folder.

Give the project name as ‘com.laymancoder.helloworld’ and click the Finish button, as shown in the video below:

What is a ‘package’ ?

A package is a group of classes. We will see later what are the classes in Java. For now, assume that a class is a text file which has our Java code and a package is a folder.
Just like a folder can have other subfolders and files in it, a package can have other subpackages and classes in it.

package structure in Java

Step 5

Under the package created in Step 4 above, create a ‘class’ as shown in the video below. Keep the class name ‘HelloWorld’. Before clicking on the Finish button, checkmark the checkbox for :

public static void main(String[] args)

Step 6

The new class must be now open in the editor window at the center of the Eclipse window. If it’s not, double click and open the HelloWorld class under the ‘src‘ folder from the package explorer section.

Write the following code in the HelloWorld file:

package com.laymancoder.helloworld;

public class HelloWorld {

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

Step 7

Once you have written the code in HelloWorld.java, run the Java code by click on the Run button in the toolbar.

run java project

If you see “Hello World!” in the output window, our first Java program is ready. We are now good to move on!

All the best!

One thought on “Writing Our First Java Program”

Leave a Reply

Your email address will not be published. Required fields are marked *