Abstract Lars Vogel <webmaster@vogella.de> Version 0.7 Copyright 2007 Lars Vogel 26.10.2007 Eclipse is a powerful, extensible IDE for building general purpose applications. One of the main applications of Eclipse is using it as a Java IDE (Integrated Development Environment). This article will describe the usage of Eclipse as a Java IDE. It contains a short introduction of the usage of Eclipse, discuss features of Eclipse which should increase the programmers productivity, lists the most important keyboard shortcuts and gives a few tips and tricks. Table of Contents 1. Installation... 2 2. Eclipse Overview... 2 2.1. Start Eclipse... 2 2.2. Workspace... 2 2.3. Eclipses User interface Elements... 2 2.4. Configuration of the perspective... 3 3. Create your first Java program in Eclipse... 3 3.1. "Hello Eclipse world!" console program... 3 4. Way of Working... 4 4.1. Powerful Time Savers... 5 4.2. Important Preference Settings... 6 4.3. Task Management... 6 4.4. Working Sets... 6 5. s... 6 5.1. Java Coding... 6 5.2. Editor... 7 5.3. Programming... 7 6. Helpful Settings... 8 6.1. Synchronize package explorer with code display... 8 6.2. Setting code and comments line widths / line length and text wrapping... 8 6.3. Local History... 8 6.4. Code Templates... 8 7. Addings jars to the build path... 9 8. Javadoc... 9 8.1. Using Javadoc in Eclipse... 9 8.2. Creating Javadoc with Eclipse... 9 9. Add source and the api description to Eclipse... 10 9.1. Add source code for external jars... 10 9.2. Add the API for a jar... 10 10. Links and Literature... 10 1
1. Installation Download Eclipse from the website http://www.eclipse.org/ and unpack it to any directly. Eclipse requires an installed Java as of version 1.4. Recommended it to use Java 6. 2. Eclipse Overview 2.1. Start Eclipse To start Eclipse double-click on the file Eclipse.exe in your installation directory. Close the welcome page by press in little x besides the Welcome. 2.2. 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). You can take parts of your settings which you did in your workspace to the next workspace. In your workspace all your sources files, images and other artefacts will be stored and saved. Note: To predefine the workspace you can use the startup parameter -data path_to_workspace, e.g. if your eclipse is located in c:\eclipse use the following to set the workscape to c:\temp. Please note that you have to put the path name into brackets. For example: c:\eclipse.exe -data "c:\temp" 2.3. Eclipses User interface Elements Eclipse provides perspectives, views and editors. Views and editors are grouped into perspectives. A perspective is a visual container for a set of views and editors (parts). Views provide information for a given task. A view is typically used to navigate a hierarchy of information, open an editor, or display properties for the active editor. Editors are meant for for the primary focus of attention and shows the main content of your application. The main differences between editor and views are not technical but semantic differences. Use editors for a task if this is a primary task. Technical differences: Editors are shared between perspectives, e.g. if you close an editor in one perspective it is closed in all perspectives. 2
You can asked for the active editor even if the editor is not in focus. This make updates of the active editor / access to the editor easier. To describe views and editors take the example of an email client. The navigation within the email client would be done via views, while the part of the application which is used for writing the email would be performed via an editor. 2.4. Configuration of the perspective You can change the layout within a perspective (close / open views, editors, change the size, change the position, etc.) A common problem at the beginning is that the perspective is accidently changed. Just use the menu "Window" -> "Reset Perspective" to set the perspective back to its original state. 3. Create your first Java program in Eclipse 3.1. "Hello Eclipse world!" console program The following will describe how to create a minimal Java program using Eclipse. Select from the menu File -> New-> Java project. Maintain "MyFirstProject" has the project name. Select Create separate source and output folders. Press finish. Open the folder, select the folder src, right mouse click on it and select create package. Create package mypackage. 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 mypackage; public class MyFirstClass { 3
} /** * @param args */ public static void main(string[] args) { System.out.println("Hello Eclipse World!"); } Now run your code. Finished! You should see the output in the console. 4. Way of Working Eclipse provides tremendous help during programming. Even though it is not possible to describe all then following tried to highlight a few nice features. The killer helpers are Content Help Quick Fix 4
Table 1. 4.1. Powerful Time Savers If you press Enter during code assistant then the cursor will go to the end of the line. Using Eclipse Europa - A Tutorial Or type in the coding Ex and then press [Ctrl + Space] to get a list of all types which starts with Ex. Or if you have type variable test. and then press [Ctrl + Space] you get all the possible methods. Allows also to define variables based on the camelcase notation. So you can type NPE and press [Ctrl+Space] to get the Types which have the three capital letters included, e.g. NullPointerException. Generated Getters and setters Quick Fix The content assistant is also available for the definition of variables. For example define a variable "private String" and the press [Ctrl+Space] to define the name automatically. By default it will use the name of the type. If you want to add a prefix / suffix you can do this under Windows-Preferences and here Java -> Code Style. To create getter and setter methods for a field, select the field's declaration and invoke Source > Generate Getter and Setter. 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. You can also use Quick Fix to create a new local / field variable for a parameter of the method. Select in the method the parameter and press Quick Fix to get the option to "Assign the parameter to new field". You can also use Quick Fix to create a method for an object. Just type the call of the method. Eclipse will indicate that this method does not exists. Press Quick Fix to get the option to create this method. You can also use Quick Fix to handle Exceptions, e.g. create the try / catch block for you, or add a through exception to the method. You can also use Quick Fix to assign a statement to a new variable. Lets assume you have the statement a.calculatesomething(); whereby calculatesomething returns an integer. Select the statement and press Quick Fix to assign it to a new local / field variable. With a Quick Fix you can also check the spelling of types words. 5
4.2. Important Preference Settings Eclipse allows to set semicolons (and other elements) automatically. Eclipse let you also configure the errors / warnings it displays. Eclipse allows to format the source code and to organize the imports at save. 4.3. Task Management If you use // TODO in the coding this indicates a task for eclipse and you find it in the task view of Eclipse. 4.4. Working Sets A common problem in Eclipse is that your data in your workspace grows and therefore your workspace is not well structured anymore. You can use working sets to organize your displayed projects / data. To setup your working set select in the Package Explorer -> Show -> Working Sets. Press new on the following dialog to create a working set. On the following dialog select java, select the source folder you would like to see and give it a name. You can now easily display only the files you want to see. 5. s 5.1. Java Coding Table 2. Important shortcuts Ctrl + S Alt + Shift + X - J Ctrl + Shift + F Ctrl + Shift + O Ctrl + Shift + R Alt + Left / Alt + right Alt + Strg + 0 Alt + Strg + 7 Save Run as Java application Format source code Organize the imports / Will import the missing imports. Refactor / Rename method / variable Go back to last position/ Go to next position } (Easier to reach then AltGr + 0) { (Easiwer to reach then AltGr + 7) 6
5.2. Editor Table 3. Important s Ctrl + Alt + Down Ctrl + D Pos1 End Ctrl + Q Ctrl + M Copy line Deletes line Line Start Line End Last edited position Full screen mode for java coding and back Table 4. Search Alt+Left Alt-Right Ctrl + Mausclick on a variable Ctrl +. F3 on a variable Strg + J, Strg +k Go to prev/ next editor position in history Go to declaration of this variable Go to previous / next problem Goto Declaration of this variable Incremental search, find next Table 5. Selecting Ctrl + Shift + Left Ctrl + Shift + Right Alt + Shift + Left Alt + Shift + Right Ctrl + Entf Ctrl + Left Ctrl + Right Select previous word Select next word Select previous element Select previous element Delete next element Move one element to the left Move one element to the right 5.3. Programming Table 6. Important shortcuts Ctrl F11 F12 Ctrl + Shift + P Alt + Shift + Z Run last launched Focus to editor window Go to matching bracket Surround block with try and catch 7
Table 7. Debugging Cntl + Shift + B F11 F5 F6 F7 Toggle breakpoint Debug last run Single Step (Down) Single Step (Jump) Up 6. Helpful Settings 6.1. Synchronize package explorer with code display On the package explorer select the arrow down menu on the right upper corner (menu). Here you find the display settings. Select the last entry "Link with Editor". This is also available on the menu via the double arrow buttons. With this selection the package explorer will always show the source file you selected in the editor. Example: if you working on foo.java and you change in the editor to bar.java then the display in the package explorer will change. 6.2. Setting code and comments line widths / line length and text wrapping Code line wrapping is set in Preferences->Java->Code Style- >Formatter, then click on the Edit button and select the Line Wrapping tab. Comment width and line wrapping is set in Preferences->Java->Code Style->Formatter, then click on the Edit button and select the Comments tab. Indentation is set separately, in Preferences->Java->Code Style- >Formatter, then click on the Edit button and select the Indentation tab. 6.3. Local History Eclipse stores for each file a local history which you can use to retrieve an older version of the file even without using a version control system. Double-click on on Revision Time in the history view to see the differences. 6.4. Code Templates If you have to type frequently the same code / part of the document you can maintain templates which can be activate via autocomplete (Ctrl + Space). For example lets assume you are using Eclipse to edit XML files and have to write the following text frequently only with a different file name. 8
<para> <programlisting> <xi:include xmlns:xi="http://www.w3.org/2001/xinclude" parse="text" href="../examples/java/wizard/mypagetwo.java" /> </programlisting> </para> To create a template for this select the menu Window->Preferences and Open Web and XML -> XML Files-> Templates Press New. Create the following template. ${cursor} indicates that the cursor should be placed at this position after applying the template. Now every time you type the keyword and press Ctrl+Space the system will proposal to add the additional text. 7. Addings jars to the build path To add external jars to your project perform the following: Create a new folder called lib or use your existing folder Select import -> file system -> import the.jar Select your project, right mouse click and select properties. Under libraries select Add JARs and under Order and Export include your jar file and move it up to avoid conflicts. The following example shows how the result would look like if junit-4.4.jar would be added to a project. 8. Javadoc 8.1. Using Javadoc in Eclipse To call the Javadoc for a type / class / method, position the cursor on the keyword and press Shift + F2. 8.2. Creating Javadoc with Eclipse To create Javadoc from Eclipse for your coding, select File -> Export. Select then Javadoc and follow the wizard. 9
The javadoc command is the javadoc.exe file which you will find in your jdk installation directory. 9. Add source and the api description to Eclipse 9.1. Add source code for external jars To browse the source of a type contained in library you can attach a source archive or source folder to this library. The editor will then show the source instead of a the decompiled code. Setting the source attachment also allows source level stepping with the debugger. The Source Attachment dialog can be reached via: Open the Java Build Path page of a project (Projects > Properties > Java Build Path). On the Libraries page expand the library's node and select the Source attachment attribute and press Edit Maintain the location to the source attachement. In the Location path field, enter the path of an archive or a folder containing the source. 9.2. Add the API for a jar Download the API of the jar and put it somewhere in your filesystem. Open the Java Build Path page of a project (Projects > Properties > Java Build Path). On the Libraries page expand the library's node and select the Javadoc location attribute and press Edit Maintain the location to the api. 10. Links and Literature http://www.eclipse.org/ - Eclipse Website [http://www.eclipse.org/] http://www.vogella.de/articles/eclipse/article.html - Using Eclipse as IDE - Tutorial www.vogella.de/articles/eclipse/article.html] [http:// http://www.vogella.de/articles/richclientplatform/article.html - Eclipse Rich Client Platform Tutorial - A Hands-on-Guide [http://www.vogella.de/articles/richclientplatform/article.html] http://www.vogella.de/articles/eclipsemicrosoftintegration/article.html - Eclipse Microsoft Integration (Outlook )- Tutorial [http://www.vogella.de/articles/eclipsemicrosoftintegration/article.html] http://www.vogella.de/articles/eclipsecodeaccess/article.html - Goto the source - A guide to access the Eclipse Sources [http://www.vogella.de/articles/eclipsecodeaccess/article.html] 10