Using Eclipse with Java to Develop Software Applications

0
11887

Eclipse is an integrated development environment (IDE) used when building software applications. It is one of the most popular, feature-rich IDEs in the Java programmer community, enabling rapid development and improving code quality. Eclipse supports all major programming languages like Java, C/C++ and Python. In this article, we’ll use Eclipse with the Java programming language.

The good thing about Eclipse is that it’s free and open source software. Developed and maintained by the Eclipse Foundation, it is released under the terms of the Eclipse Public License; so we can use it for personal as well as commercial purposes.

The salient features of Eclipse are:

  1. It supports the integration of software development tools like the compiler, debugger, version control system, and so on.
  2. It supports code navigation, code refactoring, auto code generation and has many more useful functions.
  3. It provides a rich set of plug-ins to enhance IDE functionality further. For instance, it provides a plug-in for unit-testing frameworks.
Figure 1: Eclipse window
Figure 2: The Eclipse workspace

Setting it up

Eclipse can be downloaded freely. To get the latest version, visit https://www.eclipse.org/downloads/ and follow the on-screen instructions.

Once the downloading is done, extract the .tar.gz bundle using the following command:

$ tar xvf eclipse-inst-linux64.tar.gz

That’s it — the installation is done! Now you’re ready to use Eclipse. Just go inside the extracted directory and double-click on the executable, namely, ‘eclipse’. It will launch the IDE.

Figure 3: Eclipse project
Figure 4: Eclipse class

Getting familiar with Eclipse

Let us get familiar with Eclipse’s window. After launching Eclipse, the window shown in Figure 1 will get opened.

In Figure 1:

  1. The topmost title bar shows the workspace’s name. In my case, it is ‘eclipse-workspace’.
  2. Below that, there is a menu bar which offers various functionalities.
  3. Just below that is a toolbar that provides shortcuts like compile, run, debug and much more.
  4. On the left side is a ‘Package Explorer’, which lists packages and classes.
  5. At the bottom is the console window, where output/errors are displayed.

Don’t worry if you think this is not making any sense at this moment. Later sections of this article discuss all these components, briefly.

Figure 5: Eclipse output
Figure 6: The Eclipse breakpoint

Creating your first Java program

Let us get our hands dirty with Eclipse. In this section, we’ll write the traditional ‘Hello World’ program in Eclipse. To create a new workspace, just launch Eclipse. It will ask for the name and location of the workspace. The workspace location on your machine is where the application’s code will be stored.

Let us create our first Java project. Navigate to File->New->Other, then select Java Project from the drop down list and follow the on-screen directions to create the project.

I’ve created the HelloWorld project, which we can see in the ‘Package Explorer’.

Let us create a Java package. A package is a logical container that contains related Java classes. To create a package, select the ‘src’ directory from the ‘Package Explorer’. Navigate to File->New->Package, provide the package’s name and click on the ‘Finish’ button. I’ve used ‘com.osfy’ as a package name.

Now let us create a Java class which will contain actual code. To create a class, select the newly created package from the ‘Package Explorer’. Navigate to File->New->Class, provide the class name and click on the ‘Finish’ button. This action will open a class file in the editor. After adding the main() method, the code will look like what’s shown in Figure 4.

Now let us compile and run this Java class. We can do so as a Java application. Select Class from the Package Explorer->right click ->Run As->Java Application. If everything goes well, it will show an output in the console window as seen in Figure 3.

If there are any errors, these will be listed under the ‘Problems’ tab.

Figure 7: Inspecting variables

Debugging a Java class

The debugger is a programmer’s best friend. Using it, we can stop the execution of a program at a certain point to examine variables, step into methods and do many other tasks. In this section, we’ll look at how to debug a simple Java class.

Let us create a Java class with the following code:

package com.osfy;

public class AdditionExample {

public static void main(String[] args) {

int a, b, c;

a = 10;

b = 20;

c = a + b;

System.out.println(a + “ + “ + b + “ = “ + c);

}

}

We can stop the execution of a program by setting breakpoints. To set a breakpoint, in the Editor window, double-click on the line number. Once a breakpoint is set, a small circle will be shown at that line. For instance, Figure 6 shows that breakpoint is set at line no. 9.

To start the debugger, select Class from the Package Explorer->right click->Debug As->Java Application. It will open the debugger window from which you can inspect variables.

In addition, this perspective provides a toolbar that allows you to resume execution, terminate debugging, step into a method, step over, step return and so on. For instance, Figure 8 shows that, after performing the ‘step into’ action, we can inspect the value of the variable ‘c’.

Power actions

Eclipse is a feature-rich IDE that provides many power actions which make a programmer’s life easier. In this section, we’ll look at some power actions like code generation, code formatting and so on.

1) Code generation: Eclipse allows us to generate code for the getter/setters, constructors, toString, hashcode and equals methods. To generate code, right click in the Editor window, select the source and choose the option you need.

2) Code refactoring: Eclipse allows us to perform refactoring at the project, package or class levels. To perform refactoring, select Project/Package/Class from the ‘Package Explorer’, right click on it and select the ‘Refactor’ option. It will allow various actions like rename, move, and so on.

3) Code formatting: Eclipse allows us to provide code formatting actions. Using this we can instruct Eclipse to remove unwanted spaces, indent code according to our choice, and so on. Let us unindent code deliberately, as follows:

package com.osfy;

public class AdditionExample {

public static void main(String[] args) {

int a, b, c;a = 10;b = 20;c = a + b;

System.out.println(a + “ + “ + b + “ = “ + c);

}

}

Now, to format this code, select the code block ->Right click->Source->Format. This will auto-indent the code.

Figure 8: Debugger actions

4) Organise imports: Java provides a rich set of collections. Each collection is defined in a separate class/package. Remembering each class/package’s name will be overwhelming and here Eclipse provides a solution.

To understand this, let us add the following new line into our Java class:

List<Integer> = new ArrayList<>();

To work this program correctly, we have to import the java.util.ArrayList and java.util.List packages into the current program. Obviously, we can do this manually but why bother when Eclipse can do it for you.

Now to organise imports, right click in the Editor window->Source->Organize import. You can select the appropriate option from the drop-down menu.

Useful keyboard actions

Eclipse provides shortcuts for almost every action. Let’s look at a few widely used shortcuts.

  1. To list all shortcuts, use Ctrl + Shift + L.
  2. Eclipse can provide suggestions for code completion and the shortcut for this feature is Ctrl + space. For instance, one can type ‘System’ and then press Ctrl + space.
  3. To move code up, place the cursor at any line and press Alt + up arrow key. This will move the current line up by one position. If you want to do this with multiple lines, then select the code block and press the Alt + up arrow key.
  4. To move code down, place the cursor at any line and press Alt + down arrow key. This will move the current line down by one position. If you want to do this with multiple lines, then select a code block and press Alt + down arrow key.
  5. The ‘toggle comments’ functionality is used to comment/uncomment source code. Select the code block and press Ctrl + Shift + C.
  6. To delete a single line, place the cursor at any line and press Ctrl + D. If you want to do this with multiple lines, then select the code block and press Ctrl + D.
  7. To go to a particular line, press Ctrl + L, which will lead to a window popping up, where you can enter the line number.
  8. To show the type hierarchy, use Ctrl + T.
  9. To show the outline of the current class, use Ctrl + O.
  10. To maximise or minimise an active view, use Ctrl + M.

LEAVE A REPLY

Please enter your comment!
Please enter your name here