The Eclipse IDE
- Installing The Eclipse SDK
- Creating a Hello World Application
- Some Advanced Features
- Installing a Plug-In
- Exercise Set
- An instruction given to the computer, by means of a keyboard, punch card, mouse, voice command, or other method.
- The command prompt window that rece ves output from a System.out.print call.
- A device that spins disks or tapes in order to read and write data; for example, a hard drive, floppy drive, CD-ROM drive, or tape drive.
- A mistake made by a software developer that introduces a defect in a computer program.
- The automated creation of source code.
- Integrated Development Environment, an IDE combines the editor, compiler and other useful tools in the same software package. Its advantage is that when a program with syntax errors is compiled, the programmer sees the error messages and the original program at the same time -- this makes debugging much easier.
- Refactoring is the process of rewriting written material to improve its readability or structure, with the explicit purpose of keeping its meaning or behavior.
- The rules by which the words in a program are combined to form commands to a computer.
- Syntax Highlight
- Syntax highlighting is a feature of some text editors that displays text, especially source code, in different colors and fonts according to the category of terms. This feature eases writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct.
- To zip a file is to compress it into an archive so that it occupies less disk space.
Often newcomer's to programming will find editing source files in a text editor to be tedious. Seasoned developers hardly ever use a text editor either. An Integrated Development Environment (IDE) provides the functionality to edit source code, compile it and run the source code all from within a single application.
Eclipse is an excellent open source IDE for Java. It is developed by IBM and written in Java itself. Eclipse provides a wide range of capabilities and features; we will look at this useful IDE in this chapter.
You can get Eclipse @ www.eclipse.org
Installing The Eclipse SDK
The first thing we need to do is to install Eclipse.
Installing on Windows
Extract the eclipse archive to the C:\ drive using a program like Winzip or ZipGenius. You should end up with a folder in the C: drive named eclipse.
The eclipse program is in the eclipse folder and is a file named eclipse.exe. You can double click this file to run the program. You might want to create a shortcut to this file on your desktop or in your start menu.
Installing on Linux
Copy the Eclipse SDK archive to the /usr/local directory.
cp eclipse-SDK-3.0-linux-gtk.zip /usr/local/
Change into the /usr/local directory and extract the archive.
cd /usr/local/ unzip eclipse-SDK-3.0-linux-gtk.zip
Edit the file named java.sh in the /etc/profile.d directory and add the following to it.NoteIf you installed java according to chapter 1 you should have a file named java.sh in the /etc/profile.d directory. If not, now would be an opportune time to create it.
Now add ECLIPSE_HOME to the PATH variable, save the file and exit your editor.
Log out and then log in again for the changes to take effect.
Run Eclipse: in a terminal type
In the dialog that asks you to select a workspace click the OK button. After eclipse has started, you will get a welcome screen.
Creating a Hello World Application
The best way to learn about Eclipse and its features is to start using it; we will therefore create a simple Hello World application using it.
Create a New Java Project
Start eclipse with the command:
Select a workspace from the dialog and ensure to check the box that makes it the default workspace.
In the welcome screen, click on the "Workbench" icon.
From the File menu, select New and then Project.
File > New > Project
In the New Project Dialog, click Java Project and then click the Next button.
In the New Java Project Dialog enter Hello World as the Project name and click the Finish button.
A dialog will pop up asking you to confirm the switch to the Java Perspective. Check the checkbox that reads Remember my decision and then click the Yes button.
In the Java Perspective, in the left side of the screen is the Package Explorer. In it you will see the Project named Hello World. Make Sure it is selected.
Create a New Class
Select File from the menu and click New. Select Class.
File > New > Class
In the New Java Class dialog enter HelloWorld as the name of the class, ensure that the public static void main(String args) checkbox is selected and then click the Finish button.
You'll notice in the source code editor that the class has already been created for you.
Add Source Code
In the main method, type
You'll notice a popup, prompting you to select a member of the System class. Select the out member.
Append the following to the line:
You'll notice a red squiggly line under the closing bracket. This indicates that there is an error in your code. Add a semi colon (;) and it will disappear.
Save the Code
Select Save from the File menu to save the changes to the source code. You'll notice that Eclipse compiles source code automatically, every time that you save it.
File > Save
Run the Program
Right Click on the HelloWorld.java file in the Package Explorer and select Run followed by Java Application from the pop up menu.
Run > Java Application
You'll notice the output in the console.
Some Advanced Features
Configuring Some Preferences
We will now change a few of the default preferences.
Select Preferences from the Window menu.
Window > Preferences
Click the arrow next to Workbench to expand the sub categories and select Colors and Fonts. In the right pane expand Java and then Java Editor Text Font. Click the Change button, and change the font according to your preferences.
In the left pane, expand the Java category and then the Code Style sub category. Select Code Templates. In the right pane select Comments and then Types. Click the Edit button and change the comment to your preferences.
Now select Code and then the New Java files sub category. Click the Edit button and change the comment according to your preferences.
In the left pane select Editor and select the Show line numbers check box.
Select the Syntax tab in the right pane and change the color of the keywords from mauve to blue. Click the Apply button.
Click the Ok button. You'll notice that the changes have taken effect in the source code editor.
Create a New Project
We will now create a project outside of eclipse and then import it into eclipse.
In your home directory, create some directories:
mkdir ~/test mkdir ~/test/src mkdir ~/test/classes
In Eclipse, create a new project by selecting New from the File menu and then select Project.
File > New > Project
In the dialog, ensure that Java Project is selected and click the Next button.
In the next dialog, use Test as the project name. Then click the radio button that reads Create project at external location. Click the browse button and select the test directory in your home directory. Be sure to click the Next button.
In the source folders list, select Test and click the remove button. Click the Add Folder button and select the src directory. Click the OK button.
Click the browse button to change the Default output folder and select the classes directory. Click the Ok button.
The resulting changes should be as follows:
Click the Finish button.
Create Some Packages
You'll notice the Test project in the Package Explorer. We will define a package for our project.
In the package explorer expand the Test project.
Right click on the src folder in the package explorer and select New and then Package. Add test as the package name and click the Finish button.
You will now see the empty package in the Package Explorer.
Create a New Class
We will create a simple class in the package we have just created..
Right click on the Test package. Create a new class named Cat by selecting New and then Class from the pop up menu.
Add two variables, one named age of type int and one named name of type String.
Add a simple constructor to initialize the variables.
Generate Getters and Setters
Now we will use eclipse to generate getter and setter methods for our class.
Select Generate Getters and Setters from the Source menu.
Source > Generate Getters and Setters
In the dialog click the Select All button followed by the Ok button.
You'll notice that the get and set methods were created.
Don't forget to save the file.
We would like to change the name of the package from test and have all changes reflected. First we will create a main program that will use our class.
Create a new package named com.acme.test.main
Create a program called Main in the new package.
Add the following code to the main method.
Cat kitty = new Cat()
You will again notice some squiggly red lines. In the gutter next to the line numbers you will see a hint icon. Click on it.
In the pop up hint you will be prompted to import the Cat class. Do so, by clicking on the suggestion. You will notice that an import statement has been added to your code, namely: import test.Cat. Save the class.
Right click on the test package in the Package Explorer and select refactor from the menu. Select Rename from the sub menu.
Refactor > Rename
Rename the package to com.acme.test.pets .
If you look at the main class, you'll notice that the import statement has changed to match the new package name, ie: import com.acme.test.pets.Cat!
Installing a Plug-In
Eclipse is designed in such a way that third party developers can create plug-ins for it. One such plug-in allows you to edit XML files: it provides syntax highlighting for XML and code completion.
Installing a plug-in in Eclipse is a simple and painless process. All plug-ins are distributed in zip compressed archives, much like eclipse. All you have to do is extract the archive in the correct directory within Eclipse and it should work out of the box.
For this installation, we're using the XML buddy plug-in form here.
If you open an XML file in Eclipse you will notice that Eclipse uses the default text editor for XML files. This allows you to edit the files but it doesn't provide features common to most IDE's. In this case, installing a plug-in will help.
- Close Eclipse before proceeding.
- Extract the archive to location of your choice. This results in a folder named com.objfac.xmleditor_version.
Move this folder into a folder named plugins within the eclipse folder.
Start Eclipse and open an XML document, you should see that it is now syntax highlighted.
Eclipse is an advanced and feature rich IDE for the Java language. It is simple to install and easy to learn to use. We will be using this valuable tool in the next few chapters in this book.
Eclipse is written in which language?
- Proprietary Software
- Commercial Software
- Open Source Software
Which is not a feature of Eclipse
- Eclipse allows easy refactoring of code.
- Eclipse is available for many platforms.
- Eclipse has Apache Ant integrated into it.
- Eclipse requires you to compile source code after it is saved.
- Eclipse has JUnit integrated into it.
Eclipse allows you to:
- Generate new classes and interfaces quickly and easily.
- Use third-party plug-ins.
- Generate getters and setters.
- Refactor code.
- All of the above.
Eclipse is available for:
- Sun Solaris
- 1 and 2.
- 2 and 3.
- Install the Eclipse on your own computer.
- Create the Hello World program described in this chapter.
- Import your existing projects into Eclipse.
- Configure the Preferences of Eclipse according to your taste.
- Install the plug-in as described in this chapter.
- Complete the following programs using Eclipse:
- A program that calculates how much is left of a bank loan. Input parameters are initial loan amount, interest rate, payment per month and current month.
- A program to calculate the average of a list of numbers.
- A program that calculates a student's grade from grades, each counting an equal percentage.