Java Development Using Eclipse IDE

This Tutorial Explains How To Use The Eclipse IDE for Java Development:

We will discuss all about the download, installation, development and all other operations that we can perform as far as Java development is concerned.

Eclipse is a widely used IDE primarily for Java development. Eclipse is used for C and C++ development as well as PHP among other programming languages.

Java Development using Eclipse IDE

Eclipse IDE For Java Developers

Eclipse IDE is written in Java. It mainly consists of a base ‘Workspace’ and a plug-in system so that we can add more features to it through plugins and extend the functionality of the IDE.

Eclipse works on all the major platforms including Windows, Mac OS, Linux, etc. and boasts of powerful features that can be used to develop full-fledged projects.

Development Environment for Eclipse includes:

  • Eclipse Java Development Tools (JDT) for Java and Scala.
  • Eclipse C/C++ Development Tools (CDT) for C/C++.
  • Eclipse PHP Development Tools (PDT) for PHP.

Click here for the official site of Eclipse.

In this tutorial, we will explore the features of Eclipse IDE with respect to Java development and also discuss all the steps to setup eclipse on our computer to begin development.

Features Of Eclipse IDE

  • Almost everything in Eclipse is a plugin.
  • We can extend the functionality of Eclipse IDE by adding plugins to the IDE, maybe for additional programming language or version control system or UML.
  • Supports various source knowledge tools like folding and hyperlink navigation, grading, macro definition browser, code editing with syntax highlighting.
  • Provides excellent visual code debugging tool to debug the code.
  • Eclipse has a wonderful user interface with drag and drop facility for UI designing.
  • Supports project development and administered framework for different toolchains, classic make framework, and source navigation.
  • Java Eclipse IDE has a JavaDoc facility using which we can automatically create documentation for classes in our application.

Install And Configure Eclipse Java IDE

In order to install and configure Eclipse Ide for Java development, first, we need to make sure that we have an appropriate JDK version on our machine.

Please follow the below steps to install and configure Eclipse Ide for Java Development.

Step 1: Install JDK

For Eclipse to function properly, our system should have JDK installed. Eclipse makes use of JDK for Java development.

Note: We have already covered a topic on JDK installation in our tutorial “Java Download & Install”. Please refer to it to know more about the JDK installation.

Once the applications we build using Eclipse IDE are built and executed successfully, we should have values set for two environment variables on our system i.e. “PATH” and “CLASSPATH” which are set as a result of JDK installation.

Step 2: Download Eclipse

Download the Eclipse installable from Eclipse Packages. Click on “Eclipse IDE for Java Developers” for “Windows 64-bit” which is a 201 MB file.

Step 3: Unzip

There is no installation sequence as such for Eclipse, you just have to unzip the contents of the downloaded package and then run “Eclipse.exe” and you are ready for Java development using Eclipse IDE.

Development Using Eclipse IDE

#1) Launch Eclipse

  • Eclipse can be launched by running “eclipse.exe” from the Eclipse installed directory.

The window to choose workspace is as shown below:

Select Workspace

  • Choose an appropriate directory for your workspace, i.e., where you would like to save your files (For Example, c:\Users\Username\workspace for Windows). Click Ok.
  • If the “Welcome” screen shows up, close it by clicking the “close” button next to the “Welcome” title.

#2) Create a New project

Click File -> New-> Java Project.

The following dialog box is presented to the user.

Create a Java Project

Specify the project name. Select the option “Use default JRE (currently ‘JRE’) and then click NEXT by keeping the other options unchanged.

The below dialog box is presented to the user.

Java Settings

You are shown the summary of the just created project. In this dialog, you can also specify a default output folder path. Click Finish and the project is created.

#3) Create a New Class

To start Java coding, we need to create different classes. Let us create our first class.

For this right-click Project name in the Package Explorer pane (on the left side). Select New -> Class. The following screen will be displayed.

New Class

Fill in the details as per your requirements and click Finish. A new class will be created.

#4) Write a “Hello, world” program.

Once the class is created, a corresponding source file is created for this class in the ‘src’ folder of the project and it opens in the editor. In this case, as we created a class named “HelloWorld”, a file named “HelloWorld.java” is created.

Provide the following code in the ‘HelloJava.java” file.

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

Now our first program is ready and we can move on to the “Build and execute” step.

Build And Execute Projects In Eclipse

We can build the project by right-clicking the project name in the Project Explorer and selecting “Build Project”. Actually, there is no need to compile Java projects as Eclipse employs “Incremental Compilation” i.e. each Java statement is compiled as and when it’s entered.

Once the build is successful, run or execute the project. For this, right-click the project name on the Project Explorer and click “Run as”. Then select “Java Application”. This runs your application.

The following screenshot shows the Eclipse IDE once the application is executed.

Compile & Execute

Note the output of the program in the “Console” window.

Debugging An Application In Eclipse

If you get the desired output when you run the project, then you can say that the project is successful. But if you don’t get the desired results, you might have to debug your application.

To debug a project, we have to perform the following steps:

#1) Set a Breakpoint

By setting up a breakpoint, you can suspend the execution of the program. This will allow you to examine the program step by step and also watch the intermediate values of variables and flow of execution so that you can find out the problem in your code.

It is normally a good practice to set the breakpoint in the main function as it’s the starting point for a Java program. To set a breakpoint, you can double click on the left panel of the code file against the line of code for which you want a breakpoint.

Another way is to click “Ctrl+Shift+B” by placing the cursor on the line of code for which the breakpoint is required.

debug_breakpoint

The red arrow shows the line for which the breakpoint is set. It is denoted by a circle on the left-hand pane. A red circle in the above screenshot shows the debug option in the toolbar.

#2) Start Eclipse Debugger

Once the breakpoint is set, you can start debugger by right-clicking (or Run option in the menu) the project name and select “Debug As=>Java Application”.

On doing this your execution will pause at the line at which the breakpoint is set.

#3) Step Over and Watch Variables & Output

After starting the debug, you can “Step Over” each line of code and examine the variable values by hovering your mouse over that variable.

This process of stepping through each line of code is the ultimate method to debug your program.

#4) Debug Operations -> Run-to-line resume, Terminate

These are all the operations that you can perform with debugging. Run-to-line will continue the program execution up to the line where the cursor is placed. Resume continues the program execution up to the next breakpoint or till the end of the program. Terminate terminates the debugging session.

The following screenshot shows the entire debug window of Eclipse IDE along with the operations we discussed.

Debug_Window

The debug toolbar is encircled in a red outline in the screenshot and shows icons for all the operations.

#5) Switch Back to the Development Perspective

Default Perspective

Click the Java icon shown in the above screenshot (pointed by the red arrow) to switch back to the project for further programming.

Readers can explore the other debugger features like step-into (wherein we can go inside any function and debug it); modify the value of the variable being watched, etc.

Conclusion

In this tutorial, we have seen the features, installation, configuration, and development using the Eclipse Java IDE. Although Eclipse IDE is primarily used for Java development, we can also use it for development using other programming languages like C/C++, PHP, Perl, Python to name a few.

Eclipse has a graphical debugger and hence debugging of applications becomes easier. We can develop too many advanced applications using Eclipse IDE as it’s an IDE that is easy to use.