Simple Java Program in Eclipse | Compile, Run

We can edit, compile, run, and debug Java Programs using Eclipse and NetBeans.

Eclipse and NetBeans are two free popular integrated development environments for writing Java programs. Both are easy to learn if you follow simple instructions.

We recommend that you use either one for writing Java programs. In the first section, we will use Eclipse to develop a simple Java program.

We will guide you to essential instructions step by step to new users to create a project, create a class, compile, and run a class in Eclipse.

The use of NetBeans will be introduced in the next section.

Requirements for Developing Simple Java Program in Eclipse


Following are some useful instructions needed for developing and executing a program in java:

a) Install the Eclipse IDE. If you don’t have installed it, download the Eclipse IDE and install it. For this, go to this tutorial: Download and Install Eclipse IDE for Java

b) Writing the java program.

c) Compiling java program.

d) Run java program.

Hope that you have successfully installed Eclipse IDE by following all instructions. Now, we will understand to create a java project in Eclipse.

How to create Java Project in Eclipse?


Before creating Java programs in Eclipse IDE, we need to first create a project. A project is like a folder structure that holds Java programs and all supporting files. We need to create a project only once.

Follow all the steps below to create a Java project in Eclipse:

1. First of all, open Eclipse IDE and choose File > New > Java Project to display the New Project wizard, as shown in Figure.

How to create Java Project in Eclipse IDE

2. A new project wizard will be displayed. Select java project and then click on Next button as shown in the below figure.


Creating Java project

3. Type Java Project in the Project name field. As we type, the location field will be automatically set by default. You may also customize the location for your project. Look at the below figure to understand better.

Creating Java class

4. Make sure that you chose the options Use project folder as root for sources and class files so that the .java and .class files are in the same folder for easy access.

4. Click on the Finish button to create the project, as shown in the above Figure.


Now java project is created with the src folder as shown in the below figure. The next step is to create a package folder.

Java project folder structure

How to create Java Package in Eclipse?


Follow all the steps below to create a package in java in Eclipse IDE:

1. Right-click on the newly created Java project > New > Package. A new java package wizard will be open as shown in the below figure.

How to create Java Package in Eclipse

2. Type a package name “javaProgram” and click on Finish button. A package folder named javaProgram is created on the left side.

We will learn how to create a package in java systematically in the further tutorial.

How to create Java Class in Eclipse?


After the creation of package in the java project, we can create a program in java in the java project using the following steps:

1. Right-click on package name “javaProgram” and choose File > New > Class to display the New Java Class wizard.

Creating Java class

2. Type Hello in the Name field > check the option public static void main(String[] args) as shown in the below figure.

How to create Java class

3. Click on Finish button to produce template for the program source code named Hello.java.

A class named Hello.java will be created below the package name on the left-hand side.

Now we will write the first simple java program in the template as written in the following source code.

Program source code:

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

We have developed a simple java program in the Eclipse IDE. The next step is compiling and executing a java program.

How to Compile and Run Java Program in Eclipse?


To run java program, right-click the class in the project to display a context menu. Choose Run As > Java Application in the context menu to run java program.

The output is displayed in the Console pane, as shown in the below figure.

How to compile and run Java program in Eclipse

How to Create Java Project in NetBeans?


First of all, download and install the latest Apache NetBeans in your computer system as per computer configuration. For this go to this tutorial: Download NetBeans

Before developing a Java program, we need to first create a project. Here are the steps to create a Java project in NetBeans:

1. Choose File > New Project to display the New Project dialog box.

2. Select Java in the Categories section and Java Application in the Projects section. Click on Next button to display the New Java Application dialog box.

3. Type “Java Project” in the Project Name field and browse Project Location field as you want. Uncheck Use Dedicated Folder for Storing Libraries and uncheck Create Main Class.

4. Click on the Finish button to create the project in NetBeans.

Creating a Java Class in NetBeans


After a project is developed, we can create a Java program in the project using the following steps:

1. Right-click the Java Project node in the project pane to display a context menu. Select New > Java Class to display the New Java Class dialog box.

2. Type Hello in the Class Name field and select the Source Packages in the Location field. Leave the Package field blank. It will create a class in the default package.

3. Click on Finish button to create Hello class. The source code file Hello.java is placed under the <default package> node.

4. Modify the code in the Hello class according to the above program source code.

Compiling and Running Java Program in NetBeans


To run Hello.java, right-click Hello.java to display a context menu and select Run File, or simply press Shift + F6. The output is displayed in the Output pane.

The Run File command automatically compiles the program if the program has been modified.


Hope that this tutorial has covered almost all the important points to develop a simple java program in Eclipse IDE and NetBeans. I hope that you will have understood how to create, edit, compile and run java program in Eclipse and NetBeans.
Thanks for reading!!!
Next ⇒ C vs Java

⇐ Prev Next ⇒