Chapter 2

Eclipse IDE

While your first Java program was written in a plain text editor and compiled from a command window, this is not a productive way of developing software. Professional programmers use one of the Integrated Development Environments (IDEs), which include an editor, a compiler, type-ahead help, a debugger, and a lot more (you’ll get familiar with these features later in this lesson). There several popular Java IDEs, such as Eclipse, NetBeans, IntelliJ IDEA, and RAD. Some are free, and some are not.

Eclipse is by far the most widely used IDE, and I’ll be using it for compiling and running most of the examples in this book. But switching from one IDE to another is a pretty simple process, and if you see that in some areas one IDE makes you more productive than the other, just use the best one for the job. For example, NetBeans IDE can offer great help if you’ll be developing the server-side Java EE applications explained later in the book (starting in Lesson 26).

Introducing Eclipse IDE

Eclipse IDE is an open-source product that was originally created with a substantial code donation by IBM to the Java community, and from that moment Eclipse was a 100% community-driven product. It started as an IDE for developing Java programs, but today it’s a development platform used for building thousands of tools and plug-ins. Some people are using its Rich Client Platform (RCP) API to develop user interfaces (UIs) for applications. Other Java languages are also supported by Eclipse. Visit the downloads page to see a just a portion of the Eclipse-based products available: www.eclipse.org/downloads.

Besides being an IDE, Eclipse supports plug-in development, and each developer can add only those plug-ins that he or she is interested in. For example, there is a plug-in to display UML diagrams, another offers a reporting system, and there are plug-ins for developing applications in C, Adobe Flex, and others.

Downloading and Installing Eclipse

At work, I use Eclipse IDE for Java EE Developers. Each version of Eclipse IDE has a name. At the time of this writing, the current version is called Helios. In Windows OS, my first download window looks as shown in Figure 2-1 (note jee in the file name).

The installation of Eclipse IDE comes down to a simple unzipping of the downloaded file into a disk drive of your choice. You’ll find the file eclipse.exe in the Eclipse folder — just run this program. You’ll immediately see a pop-up window asking you to select a workspace, which is a directory on your hard disk where one or more of your projects is going to be stored.

If you want to create an application in Eclipse, you start with creating a project. In the real world, the source code of a decent-size application can consist of several Eclipse projects.

For this book I selected the following workspace directory: c:PracticalJavaworkspace.

Eclipse Java EE IDE starts with a Welcome panel — just close it by clicking the little x on the Welcome tab. Figure 2-2 is a snapshot of the freshly installed IDE with an empty workspace.

To be precise, you are looking at Java EE perspective (note the Java EE tab at the top), which is a collection of views. On the left you see a Project Explorer view. The area in the middle is reserved for the code editor view — you start using it as soon as you create your first Java class. The Outline view is on the right — you’ll see the names of your classes, methods, and variables (see Lesson 3) there when they are available.

There are many other views that you can open and close by yourself by selecting Window Show View. These include Console, Search, Servers and others.

Since you are just starting to learn the language, there is no need to work in the Java EE perspective — you can get by in the Java perspective. Click the little icon with the plus sign by the Java EE tab and select “Java perspective.” You’ll see a slightly different set of views with the Package Explorer and Hierarchy views on the left, Task List on the right, and Problems, Javadoc and Declaration tabs at the bottom, as shown in Figure 2-3.

Creating Hello Project in Eclipse

In Lesson 1 you simply created the class HelloWorld, but in Eclipse you have to create the project first. Select File New Java Project and enter Hello as the name of the project in the pop-up window, as shown in Figure 2-4.

You can select the version of the JRE you want to work with. In Lesson 1 I’ve installed the JDK and JRE of version 1.6.0_19, but you may have more than one version of JRE, and Eclipse can compile the code that will run in another version of JRE. This is a pretty nice feature of Eclipse IDE — it doesn’t come with its own JRE, but it allows you to pick the version that fits your needs. In some cases you have to compile the code with older versions of JRE if you know that this program will have to run in the older JVMs.

After you click Next, the next window will ask you to specify the folder where compiled Java classes of the Hello project should be created. By default, Eclipse creates a Hello folder for this project with a bin sub-folder for compiled classes and an src sub-folder for the source code. In Lesson 1 both HelloWorld.java and HelloWorld.class were sitting in the same folder, which is a bad practice.

Don’t change the name of the output directory — just click Finish on that window. In Figure 2-5 you see a new project, Hello, in the Package Explorer view of Eclipse.

This project has an empty folder, src — you can save the source code of HelloWorld.java there when ready. The JRE folder contains all required libraries supporting the JVM where HelloWorld will run.

These library files have .jar in their names. Java SDK comes with a jar utility that allows you to create a file archive that contains one or more compiled classes. While the JRE folder contains classes created by developers of the JRE itself, most real-world applications consist of groups of files (packages) located in one or more jars.

It doesn’t make much sense to put the only HelloWorld class inside the jar, but as your sample applications grow, you’ll see how to group and compress files in jars.

Creating the HelloWorld Class in Eclipse

Our Hello project will contain one Java class: HelloWorld from Lesson 1. Select File New Class and enter HelloWorld in the Name field in the pop-up window shown in Figure 2-6.

Then enter com.practicaljava.lesson2 in the Package field. This is a new addition that was not present in the previous version of HelloWorld.

Java Packages

Packages in Java are used to better organize multi-file projects and for data protection. It’s not unusual for a project to have several hundreds of Java classes. Keeping them all in one directory is never a good idea. Hence the files will be located in various directories and sub-directories.

What are the naming conventions for packages? Java developers use so-called reverse-domain name conventions. Let’s say you work on a project called Sales for a company called Acme, which has an Internet site at acme.com. Then every package name will start with the reverse URL of the company, followed by the project name: com.acme.sales.

All Java classes that belong to this package would be stored in the following directory structure: com/acme/sales.

If some of the Java classes are specific to domestic sales, while others are used in international sales, you will create two more sub-directories: com/acme/sales/domestic and com/acme/sales/international.

While directory names are separated by a forward slash or backslash, the corresponding Java packages are separated with periods. Java has a special keyword package, and its declaration has to be the first line of the class (program comments don’t count). For example:

package com.acme.sales.domestic;

Let’s assume that you work for a company called Practical Java on project Lesson2; the name of the package will be com.practicaljava.lesson2, which is exactly what I’ve entered in the Package field shown in Figure 2-6.

Besides being used for better organization of Java classes, packages help in controlling data access. You learn about this feature in the section “Access Levels” in Lesson 6.

Completing Code Generation

You may have noticed that I also checked off the box asking Eclipse to generate the main method for me.

Click Finish, and in a couple of seconds Eclipse will generate for you the initial code for the class HelloWorld, as shown in Figure 2-7.

The generated code is shown in Eclipse’s editor view. It starts with the package statement, and the class declaration with the method name goes next.

Type in the line System.out.println("Hello World"); under the TODO comment (comments are explained in the next section), and save the code by pressing the little diskette image on the toolbar or using the Ctrl+S key combination.

As you type in the code, Eclipse displays context-sensitive help suggesting a selection of possible values, minimizing guesswork and typing errors. I made a snapshot, shown in Figure 2-8, right after entering System.o:

By default, saving the code results in an invocation of the compiler. If you didn’t make any syntax errors, Eclipse will create HelloWorld.class in the bin directory of the Hello project. In case of compilation errors, Eclipse puts a little red round bullet in front of problematic lines.

Now you can run the program by pressing the round green button on the toolbar. The output of the program will be shown in the Console view panel in the lower part of Eclipse IDE, as in Figure 2-9.

The format of this book doesn’t have space for more detailed coverage of all the features of Eclipse. The good news is that there are plenty of tutorials describing every feature of Eclipse at www.eclipse.org/documentation.

Try It

In this lesson your goal is to write, compile, and run HelloWorld in Eclipse IDE.

Lesson Requirements

For this lesson download and install the current version of Eclipse IDE for Java EE Developers from www.eclipse.org/downloads.

Step-by-Step

1. Create the Hello project in Eclipse IDE.

2. Create a new Java class, HelloWorld, with the method main(), as described earlier.

3. Compile the program by clicking Save.

4. Run the program by clicking the green button in the Eclipse toolbar.

cd.ai

Please select Lesson 2 on the DVD with the print book, or watch online at www.wrox.com/go/fainjava to view the video that accompanies this lesson.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.135.21