OpenSource For You

Using Eclipse with Java to Develop Software Applicatio­ns

- By: Narendra K. The author is a FOSS enthusiast. He can be reached at narendra00­02017@gmail.com.

rop

Eclipse is an integrated developmen­t environmen­t (IDE) used when building software applicatio­ns. It is one of the most popular, feature-rich IDEs in the Java programmer community, enabling rapid developmen­t and improving code quality. Eclipse supports all major programmin­g languages like Java, C/C++ and Python. In this article, we’ll use Eclipse with the Java programmin­g 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 integratio­n of software developmen­t tools like the compiler, debugger, version control system, and so on.

2. It supports code navigation, code refactorin­g, auto code generation and has many more useful functions.

3. It provides a rich set of plug-ins to enhance IDE functional­ity further. For instance, it provides a plug-in for unit-testing frameworks.

Setting it up

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

Once the downloadin­g is done, extract the .tar.gz bundle using the following command:

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

That’s it — the installati­on 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.

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 functional­ities.

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.

Creating your first Java program

Let us get our hands dirty with

Eclipse. In this section, we’ll write the traditiona­l ‘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 applicatio­n’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 applicatio­n. Select Class from the Package Explorer->right click ->Run As->Java Applicatio­n. 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.

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 AdditionEx­ample { 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 breakpoint­s. 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 Applicatio­n. It will open the debugger window from which you can inspect variables.

In addition, this perspectiv­e 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, constructo­rs, 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 refactorin­g: Eclipse allows us to perform refactorin­g at the project, package or class levels. To perform refactorin­g, 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 deliberate­ly, as follows:

package com.osfy; public class AdditionEx­ample { 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 autoindent the code.

4) Organise imports: Java provides a rich set of collection­s. Each collection is defined in a separate class/package. Rememberin­g each class/package’s name will be overwhelmi­ng 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 appropriat­e 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 suggestion­s 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’ functional­ity 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.

 ??  ??
 ??  ??
 ??  ?? Figure 3: Eclipse project
Figure 3: Eclipse project
 ??  ?? Figure 2: The Eclipse workspace
Figure 2: The Eclipse workspace
 ??  ?? Figure 1: Eclipse window
Figure 1: Eclipse window
 ??  ?? Figure 5: Eclipse output
Figure 5: Eclipse output
 ??  ?? Figure 4: Eclipse class
Figure 4: Eclipse class
 ??  ?? Figure 8: Debugger actions
Figure 8: Debugger actions
 ??  ?? Figure 7: Inspecting variables
Figure 7: Inspecting variables
 ??  ?? Figure 6: The Eclipse breakpoint
Figure 6: The Eclipse breakpoint

Newspapers in English

Newspapers from India