Us­ing Eclipse with Java to De­velop Soft­ware Ap­pli­ca­tions

OpenSource For You - - Contents - By: Naren­dra K. The au­thor is a FOSS en­thu­si­ast. He can be reached at naren­


Eclipse is an in­te­grated de­vel­op­ment en­vi­ron­ment (IDE) used when build­ing soft­ware ap­pli­ca­tions. It is one of the most pop­u­lar, fea­ture-rich IDEs in the Java pro­gram­mer com­mu­nity, en­abling rapid de­vel­op­ment and im­prov­ing code qual­ity. Eclipse sup­ports all ma­jor pro­gram­ming lan­guages like Java, C/C++ and Python. In this ar­ti­cle, we’ll use Eclipse with the Java pro­gram­ming lan­guage.

The good thing about Eclipse is that it’s free and open source soft­ware. De­vel­oped and main­tained by the Eclipse Foun­da­tion, it is re­leased un­der the terms of the Eclipse Pub­lic Li­cense; so we can use it for per­sonal as well as com­mer­cial pur­poses.

The salient fea­tures of Eclipse are:

1. It sup­ports the in­te­gra­tion of soft­ware de­vel­op­ment tools like the com­piler, de­bug­ger, ver­sion con­trol sys­tem, and so on.

2. It sup­ports code navigation, code refac­tor­ing, auto code gen­er­a­tion and has many more use­ful func­tions.

3. It pro­vides a rich set of plug-ins to en­hance IDE func­tion­al­ity fur­ther. For in­stance, it pro­vides a plug-in for unit-test­ing frame­works.

Set­ting it up

Eclipse can be down­loaded freely. To get the lat­est ver­sion, visit https://www.­loads/ and fol­low the on-screen in­struc­tions.

Once the down­load­ing is done, ex­tract the .tar.gz bun­dle us­ing the fol­low­ing com­mand:

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

That’s it — the in­stal­la­tion is done! Now you’re ready to use Eclipse. Just go in­side the ex­tracted di­rec­tory and dou­ble-click on the ex­e­cutable, namely, ‘eclipse’. It will launch the IDE.

Get­ting fa­mil­iar with Eclipse

Let us get fa­mil­iar with Eclipse’s win­dow. Af­ter launch­ing Eclipse, the win­dow shown in Fig­ure 1 will get opened.

In Fig­ure 1:

1. The top­most ti­tle bar shows the workspace’s name. In my case, it is ‘eclipse-workspace’.

2. Be­low that, there is a menu bar which of­fers var­i­ous func­tion­al­i­ties.

3. Just be­low that is a tool­bar that pro­vides short­cuts like com­pile, run, de­bug and much more.

4. On the left side is a ‘Pack­age Ex­plorer’, which lists pack­ages and classes.

5. At the bot­tom is the con­sole win­dow, where out­put/er­rors are dis­played. Don't worry if you think this is not mak­ing any sense at this mo­ment. Later sec­tions of this ar­ti­cle dis­cuss all these com­po­nents, briefly.

Cre­at­ing your first Java pro­gram

Let us get our hands dirty with

Eclipse. In this sec­tion, we’ll write the tra­di­tional ‘Hello World’ pro­gram in Eclipse. To cre­ate a new workspace, just launch Eclipse. It will ask for the name and lo­ca­tion of the workspace. The workspace lo­ca­tion on your ma­chine is where the ap­pli­ca­tion’s code will be stored.

Let us cre­ate our first Java project. Nav­i­gate to File->New->Other, then se­lect Java Project from the drop down list and fol­low the on-screen di­rec­tions to cre­ate the project.

I’ve cre­ated the Hel­loWorld project, which we can see in the ‘Pack­age Ex­plorer’.

Let us cre­ate a Java pack­age. A pack­age is a log­i­cal con­tainer that con­tains re­lated Java classes. To cre­ate a pack­age, se­lect the ‘src’ di­rec­tory from the ‘Pack­age Ex­plorer’. Nav­i­gate to File->New->Pack­age, pro­vide the pack­age’s name and click on the ‘Fin­ish’ but­ton. I’ve used ‘com.osfy’ as a pack­age name.

Now let us cre­ate a Java class which will con­tain ac­tual code. To cre­ate a class, se­lect the newly cre­ated pack­age from the ‘Pack­age Ex­plorer’. Nav­i­gate to File->New->Class, pro­vide the class name and click on the ‘Fin­ish’ but­ton. This ac­tion will open a class file in the editor. Af­ter adding the main() method, the code will look like what’s shown in Fig­ure 4.

Now let us com­pile and run this Java class. We can do so as a Java ap­pli­ca­tion. Se­lect Class from the Pack­age Ex­plorer->right click ->Run As->Java Ap­pli­ca­tion. If ev­ery­thing goes well, it will show an out­put in the con­sole win­dow as seen in Fig­ure 3.

If there are any er­rors, these will be listed un­der the ‘Prob­lems’ tab.

De­bug­ging a Java class

The de­bug­ger is a pro­gram­mer’s best friend. Us­ing it, we can stop the ex­e­cu­tion of a pro­gram at a cer­tain point to ex­am­ine vari­ables, step into meth­ods and do many other tasks. In this sec­tion, we’ll look at how to de­bug a sim­ple Java class.

Let us cre­ate a Java class with the fol­low­ing code:

pack­age com.osfy; pub­lic class Ad­di­tionEx­am­ple { pub­lic static void main(String[] args) { int a, b, c; a = 10; b = 20; c = a + b;

Sys­tem.out.println(a + “+ “+ b + “= “+ c);



We can stop the ex­e­cu­tion of a pro­gram by set­ting break­points. To set

a break­point, in the Editor win­dow, dou­ble-click on the line num­ber. Once a break­point is set, a small cir­cle will be shown at that line. For in­stance, Fig­ure 6 shows that break­point is set at line no. 9.

To start the de­bug­ger, se­lect Class from the Pack­age Ex­plorer->right click>De­bug As->Java Ap­pli­ca­tion. It will open the de­bug­ger win­dow from which you can in­spect vari­ables.

In ad­di­tion, this per­spec­tive pro­vides a tool­bar that al­lows you to re­sume ex­e­cu­tion, ter­mi­nate de­bug­ging, step into a method, step over, step re­turn and so on. For in­stance, Fig­ure 8 shows that, af­ter per­form­ing the ‘step into’ ac­tion, we can in­spect the value of the vari­able ‘c’.

Power ac­tions

Eclipse is a fea­ture-rich IDE that pro­vides many power ac­tions which make a pro­gram­mer’s life eas­ier. In this sec­tion, we’ll look at some power ac­tions like code gen­er­a­tion, code for­mat­ting and so on.

1) Code gen­er­a­tion: Eclipse al­lows us to gen­er­ate code for the get­ter/ set­ters, con­struc­tors, toString, hash­code and equals meth­ods. To gen­er­ate code, right click in the Editor win­dow, se­lect the source and choose the op­tion you need.

2) Code refac­tor­ing: Eclipse al­lows us to per­form refac­tor­ing at the project, pack­age or class lev­els. To per­form refac­tor­ing, se­lect Project/ Pack­age/Class from the ‘Pack­age Ex­plorer’, right click on it and se­lect the ‘Re­fac­tor’ op­tion. It will al­low var­i­ous ac­tions like re­name, move, and so on.

3) Code for­mat­ting: Eclipse al­lows us to pro­vide code for­mat­ting ac­tions. Us­ing this we can in­struct Eclipse to re­move un­wanted spa­ces, in­dent code ac­cord­ing to our choice, and so on. Let us unin­dent code de­lib­er­ately, as fol­lows:

pack­age com.osfy; pub­lic class Ad­di­tionEx­am­ple { pub­lic static void main(String[] args) { int a, b, c;a = 10;b = 20;c = a + b;

Sys­tem.out.println(a + “+ “+ b + “= “+ c);



Now, to for­mat this code, se­lect the code block ->Right click>Source->For­mat. This will au­toin­dent the code.

4) Or­gan­ise im­ports: Java pro­vides a rich set of col­lec­tions. Each col­lec­tion is de­fined in a sep­a­rate class/pack­age. Re­mem­ber­ing each class/pack­age’s name will be over­whelm­ing and here Eclipse pro­vides a so­lu­tion.

To un­der­stand this, let us add the fol­low­ing new line into our Java class:

List<In­te­ger> = new Ar­rayList<>();

To work this pro­gram cor­rectly, we have to im­port the java.util.Ar­rayList and java.util.List pack­ages into the cur­rent pro­gram. Ob­vi­ously, we can do this man­u­ally but why bother when Eclipse can do it for you.

Now to or­gan­ise im­ports, right click in the Editor win­dow->Source-

>Or­ga­nize im­port. You can se­lect the ap­pro­pri­ate op­tion from the drop-down menu.

Use­ful key­board ac­tions

Eclipse pro­vides short­cuts for al­most every ac­tion. Let’s look at a few widely used short­cuts.

1) To list all short­cuts, use Ctrl +

Shift + L.

2) Eclipse can pro­vide sug­ges­tions for code com­ple­tion and the short­cut for this fea­ture is Ctrl + space. For in­stance, one can type ‘Sys­tem’ and then press Ctrl + space.

3) To move code up, place the cur­sor at any line and press Alt + up ar­row key. This will move the cur­rent line up by one po­si­tion. If you want to do this with mul­ti­ple lines, then se­lect the code block and press the Alt + up ar­row key.

4) To move code down, place the cur­sor at any line and press Alt + down ar­row key. This will move the cur­rent line down by one po­si­tion. If you want to do this with mul­ti­ple lines, then se­lect a code block and press Alt + down ar­row key.

5) The ‘tog­gle com­ments’ func­tion­al­ity is used to com­ment/un­com­ment source code. Se­lect the code block and press Ctrl + Shift + C.

6) To delete a sin­gle line, place the cur­sor at any line and press Ctrl

+ D. If you want to do this with mul­ti­ple lines, then se­lect the code block and press Ctrl + D.

7) To go to a par­tic­u­lar line, press Ctrl + L, which will lead to a win­dow pop­ping up, where you can en­ter the line num­ber.

8) To show the type hi­er­ar­chy, use

Ctrl + T.

9) To show the out­line of the cur­rent

class, use Ctrl + O.

10) To max­imise or min­imise an ac­tive

view, use Ctrl + M.

Fig­ure 3: Eclipse project

Fig­ure 2: The Eclipse workspace

Fig­ure 1: Eclipse win­dow

Fig­ure 5: Eclipse out­put

Fig­ure 4: Eclipse class

Fig­ure 8: De­bug­ger ac­tions

Fig­ure 7: In­spect­ing vari­ables

Fig­ure 6: The Eclipse break­point

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.