Friday, September 16, 2011

Eclipse : Getting Started

1. Eclipse Overview

Most people know Eclipse as an integrated development environment (IDE) for Java. Eclipse is created by an Open Source community and is used in several different areas, e.g. as development environment for Java or Android or as a platform for Eclipse RCP applications.
The usage of Eclipse as a Java development environment will be described in this tutorial.

2. Getting started

2.1. Installation

Eclipse requires an installed Java Runtime. I recommended to use Java 6 (also known as Java 1.6).
Download "Eclipse IDE for Java Developers" from the website Eclipse Downloads and unpack it to a directory. Use a directory path which does not contain spaces in its name as Eclipse sometimes have problems with that. After unpacking the download Eclipse is ready to be used; no additional installation procedure is required.

2.2. Start Eclipse

To start Eclipse double-click on the file "eclipse.exe" (Microsoft Windows) or eclipse (Linux / Mac) in the directory you unpacked Eclipse. The system will prompt you for a workspace. The workspace is the place there you store your Java projects (more on workspaces later). Select an empty directory and press Ok.

Eclipse will start and show the Welcome page. Close the welcome page by press the "X" besides the "Welcome".

3. Eclipse UI Overview

Eclipse provides perspectives, views and editors. Views and editors are grouped into perspectives. All projects are located in a workspace.

3.1. Workspace

The workspace is the physical location (file path) you are working in. You can choose the workspace during startup of eclipse or via the menu (File-> Switch Workspace-> Others). All your projects, sources files, images and other artifacts will be stored and saved in your workspace.
You can predefine the workspace via the startup parameter -data path_to_workspace, e.g. "c:\eclipse.exe -data "c:\temp" Please note that you have to put the path name into brackets. To see the current workspace directory in the title of Eclipse use -showLocation as additional parameter.

3.2. Perspective

A perspective is a visual container for a set of views and editors. You can change the layout within a perspective (close / open views, editors, change the size, change the position, etc.). Eclipse allow you to switch to another perspective via the menu Window->Open Perspective -> Other. For Java development you usually use the "Java Perspective".

Tip

A common problem is that you closed a view and don't know how to re-open this view. You can reset a perpective it to it original state via the menu "Window" -> "Reset Perspective".

3.3. Views and Editors

A view is typically used to navigate a hierarchy of information or to open an editor. Changes in a view are directly applied to the underlying data structure. Editors are used to modify elements. Editors can have code completion, undo / redo, etc. To apply the changes in an editor to the underlying resources, e.g. Java source file, you usually have to save.

4. Create your first Java program

The following will describe how to create a minimal Java program using Eclipse. It will be the classical "Hello World" program. Our program will write "Hello Eclipse!" to the console.

4.1. Create project

Select from the menu File -> New-> Java project. Maintain "de.vogella.eclipse.ide.first" as the project name. Select "Create separate source and output folders".

Press finish to create the project. A new project is created and displayed as a folder. Open the folder "de.vogella.eclipse.ide.first"

4.2. Create package

Create now a package. A good convention is to use the same name for the top package as the project. Create therefore the package "de.vogella.eclipse.ide.first".
Select the folder src, right mouse click on it and select New -> Package.


4.3. Create Java class

Right click on your package and select New -> Class

Create MyFirstClass, select the flag "public static void main (String[] args)"

Maintain the following code.

package de.vogella.eclipse.ide.first;

public class MyFirstClass {

 public static void main(String[] args) {
  System.out.println("Hello Eclipse!");
 }

}
   

4.4. Run your project in Eclipse

Now run your code. Right click on your Java class and select Run-as-> Java application

Finished! You should see the output in the console.

4.5. Run your Java program outside Eclipse (create jar file)

To run your Java program outside of Eclipse you need to export it as a jar file. Select your project, right click on it and select "Export".

Select JAR file, select next. Select your project and maintain the export destination and a name for the jar file. I named it "myprogram.jar".


Press finish. This will create a jar file in your select output directory.

4.6. Run your program outside Eclipse

Open a command shell, e.g. under Microsoft Windows select Start -> Run and type in cmd. This should open a consle.
Switch to your output directory, e.g. by typing cd path, e.g. if you jar is located in "c:\temp" type "cd c:\temp".
To run this program you need to include the jar file into your classpath. See Classpath and Java JAR Files for details.

java -classpath myprogram.jar de.vogella.eclipse.ide.first.MyFirstClass
   

Congratulations! You created your first Java project, a package a tiny Java program and you ran this program inside Eclipse and outside

5. Content Assists and Quick Fix


Tip

For a list of the most important Eclipse shortcuts please see Eclipse Shortcuts

5.1. Content assist

The content assistant allows you to get input help in an editor. It can be invoked by CTRL + Space.
For example type syso and then press [Ctrl + Space] and it will be replaced by System.out.println(""). Or if you have an object, e.g. Person P and need to see the methods of this object you can type p. (or press CTRL + Space) which activates also the content assist.

5.2. Quick Fix

Whenever there is a problem Eclipse will underline the problematic place in the coding. Select this and press (Ctrl+1)
For example type "myBoolean = true;" If myBoolean is not yet defined, Eclipse will highlight it as an error. Select the variable and press "Ctrn+1", then Eclipse will suggest to create a field or local variable.
Quick Fix is extremely powerful, it allows you to create new local / field variables, new methods, classes, put try and catch around your exceptions, assign a statement to a variable etc.

6. Using jars (libraries)

6.1. Adding external library (.jar ) to the Java classpath

The following describes how to add external jars to your project.
The following assumes you have a jar available.

Tip

If you need an example for working with jars you can use JFreeChart Tutorial
Create a new Java project "de.vogella.eclipse.ide.jars". Create a new folder called "lib" (or use your existing folder) by right click on your project and selecting New -> Folder

From the menu select File -> Import -> File system. Select your jar and select the folder lib as target.
Select your project, right mouse click and select properties. Under libraries select "Add JARs".

The following example shows how the result would look like if junit-4.4.jar would be added to a project.

No comments:

Post a Comment