Working with Eclipse – Basics

In this article we would go through basics of working with Eclipse IDE. At the end of this post you should be able to run a simple java program using eclipse. We assume that you have Java and Eclipse installed on your machine.We will create a Java project and a Java class that will print “Hello world!” .Let’s get started!

How to create a new  Java project ?

We need a create a java project. In the main toolbar, click on the New -> Java Project button. Below picture shows how to create a new project.

JavaProject

 

 

New Project

Enter project name Sample for the project name in the dialog box , then click Finish. However, you may choose any name of your choice. Attached below screenshot for your reference.

SampleProject

 

How to create a new  Java class?

The next step is to create a New class. In the main toolbar, click on the New Java Class button. Enter “HelloWorld” for the class name, select the checkbox to create the main() method, then click Finish.The main() method would be created for you by checking this option in the dialog box.

It’s encouraged to specify the package for your class. We have specified “com.sample.demo” as shown in the below screen.In simple terms, packages are folders that hold java classes for better organizing and to avoid naming conflicts. You cannot have “HelloWorld” class twice in the same package  “com.sample.demo”. However, you can have  “HelloWorld” class in a different package let say “com.sample.practice” .

 

JavaClass

 

New Java Class

The created class would look something like below snippet :

package com.sample.demo;

public class HelloWorld {

  public static void main(String[] args) {
    // TODO Auto-generated method stub

  }
}

 

Now that you have your class, in the main() method, add the following statement to print “Hello World!” on the console.Then save your changes File -> Save or by combination of keys Ctrl + S

 

System.out.println("Hello world!");

The complete program is now :

package com.sample.demo;

public class HelloWorld {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Hello world!");
  }

}

 

// TODO Auto-generated method stub

The above line in the program is a single line comment auto generated and doesn’t interfere with the program. Comments add more readability to the programs.

To run your application, right-click on your class in the Package Explorer and select Run As > Java Application. The Console view should appear at the bottom and display the “Hello, world!” output.

RunAsJavaApplication

 

Run the java application

After the java application run , you would see the console output that displays “Hello World!”.

Hello World console output.

To enable console window in the eclipse ,  go to main toolbar Windows –> Show View –> Console.

Hooray! You have successfully created a Hello World Java application!

Related Posts