Eclipse IDE Tutorial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

419

Eclipse IDE Tutorial


Lars Vogel
Version 3.3 Copyright 2007, 2008, 2009, 2010, 2011, 2012 Lars Vogel 09.08.2012
Revision History Revision 0.1 18.07.2007 Lars Vogel

Free tutorial, donate to support

Training Books

by Lars Vogel

Created Revision 0.2 - 3.3 18.05.2008 - 09.08.2012 Lars Vogel

This tutorial is part of thi Kindle book:

bugfixes and enhancements

Eclipse Java IDE This tutorial describes the usage of Eclipse as a Java IDE. It describes the installation of Eclipse, the creation of Java programs and tips for using Eclipse. This tutorial is based on the Eclipse 4.2 (Juno).

Table of Contents
1. What is Eclipse? 2. Eclipse Public License 3. Eclipse Installation 3.1. Java Requirements of Eclipse 3.2. Installation of Java 3.3. Download Eclipse 3.4. Install Eclipse 4. Getting started 4.1. Starting Eclipse 4.2. Appearance 5. Eclipse user interface overview 5.1. Workspace 5.2. Eclipse projects 5.3. Parts 5.4. Perspective 6. Eclipse Java perspective 6.1. Toolbar 6.2. Useful Views 6.3. Opening a class 7. Create your first Java program 7.1. Create project 7.2. Create package 7.3. Create Java class 7.4. Run your project in Eclipse 8. Run Java program outside Eclipse 8.1. Create jar file 8.2. Run your program outside Eclipse 9. Exporting and importing projects

9.1. Exporting projects 9.2. Importing projects 10. Content Assist, Quick Fix and Class Navigation 10.1. Content assist 10.2. Quick Fix 11. Opening a class 12. Generating code 13. Refactoring 13.1. Refactoring 13.2. Refactoring in Eclipse 14. Exercise:Refactoring 14.1. Preparation 14.2. Perform refactoring 15. Eclipse Shortcuts 16. Using project dependencies 17. Using jars (libraries) 17.1. Adding a library (.jar) to your project 17.2. Attach source code to a Java library 17.3. Add the Javadoc for a jar 18. Updates and Installation of Plug-ins 18.1. Eclipse Update Manager 18.2. Eclipse Marketplace 18.3. Restarting Eclipse 19. Advanced Eclipse Update manager options 19.1. Manual installation of plug-ins (dropins folder) 19.2. Exporting and importing the installed components 19.3. Installing features via the command line 20. Eclipse Java development preferences 20.1. Overview 20.2. Automatic placement of semicolon 20.3. Auto escape text pasted into Strings 20.4. Bracket highlighting 20.5. Activate Save Actions 20.6. Filter import statements 20.7. Completion overwrites and insert guessed method arguments 21. More on preferences 21.1. Launch Configuration 21.2. Configuring the editors for a file extension 21.3. Export / Import Preferences 22. Using and configuring template vogella.com Tutorials Training Services Publications 22.1. Templates 22.2. Code Templates 23. Organizing your workspace 23.1. Overview 23.2. Working Sets 23.3. Task Management 24. Eclipse command line configuration 24.1. Eclipse startup parameters 24.2. Eclipse initialization file 24.3. Default values for preference settings 25. Eclipse online resources 25.1. Online documentations 25.2. Webresources 26. Reporting Eclipse bugs and asking questions
Connect

11/5/12

Eclipse IDE Tutorial


26.1. Asking (and answering) questions 26.2. Eclipse Bugs 27. Next steps 28. Thank you 29. Questions and Discussion 30. Links and Literature 30.1. Source Code 30.2. Eclipse Resources 30.3. Eclipse Bug Tracker and Eclipse Forum 30.4. vogella Resources

1. What is Eclipse?
Most people know Eclipse as an integrated development environment (IDE) for Java. Today it is the leading development environment for Java with a market share of approximately 65%.
BACK TO TOP

Eclipse is created by an Open Source community and is used in several different areas, e.g. as a development environment for Java or Android applications. Eclipse's roots go back to 2001. The Eclipse Open Source community has over 200 Open Source projects covering different aspects of software development. The Eclipse projects are governed by the Eclipse Foundation. The Eclipse Foundation is a non-profit, member supported corporation that hosts the Eclipse Open Source projects and helps to cultivate both an Open Source community and an ecosystem of complementary products and services. The Eclipse IDE can be extended with additional software components. Eclipse calls these software components plug-ins. Several Open Source projects and companies have extended the Eclipse IDE. It is also possible to use Eclipse as a basis for creating general purpose applications. These applications are known as Eclipse Rich Client Platform (Eclipse RCP) applications.

2. Eclipse Public License


The Eclipse Public License (EPL) is an Open Source software license used by the Eclipse Foundation for its software. The EPL is designed to be business-friendly. EPL licensed programs can be used, modified, copied and distributed free of charge and receiver of EPL-licensed software can choose to use this software in closed source programs. Only modifications in the original EPL code must be released. The Eclipse Foundation also validates that source code contributed to Eclipse projects is free of Intellectual property (IP) issues. This process is known as IP cleansing. The permissive EPL and the IP cleansing effort of the Eclipse Foundation makes reusing the source code of Eclipse projects attractive.

3. Eclipse Installation
3.1. Java Requirements of Eclipse
Eclipse requires an installed Java Runtime. Eclipse 4.2 requires at least Java 5 to run. For this tutorial you should use Java in version 6 or higher. The Eclipse IDE contains its own Java compiler. The Java Development Tools are required if you compile Java source code outside Eclipse and for advanced development scenarios. For example if you use automatic builds or if you develop web development.

3.2. Installation of Java


Java might already be installed on your machine. You can test this by opening a console (if you are using Windows: Win+R, enter cmd and press Enter) and by typing in the following command:
www.vogella.com/articles/Eclipse/article.html 3/46

jv -eso aa vrin

If Java is correctly installed, you should see some information about your Java installation. If the command line returns the information that the program could not be found, you have to install Java. A Google search for How to install JDK on Y U _ Sshould result in helpful links. Replace Y U _ Swith ORO ORO your operating system, e.g. Windows, Ubuntu, Mac OS X, etc.

3.3. Download Eclipse


The Eclipse.org website provides pre-packaged Eclipse distributions. Download the Eclipse IDE for Java Developers package from the following URL:
ht:/w.cis.r/onod tp/wwelpeogdwlas

The following screenshot shows the Eclipse download website for a Linux system, press on the link beside the package, e,g, Linux 64 Bit to start the download.

The download is a . i file. zp

3.4. Install Eclipse


After you downloaded the . i file which contains the Eclipse distribution you unpack it to a local directory. zp Most operating system can extract zip files in their file browser, e.g. Windows7 via right mouse click on the file and selecting "Extract all...". If in doubt, search via Google for "How to unzip a file on ...", again replacing "..." with your operating system. Use a directory path which does not contain spaces in its name, as Eclipse sometimes has problems with that. After unpacking the downloaded zip file, Eclipse is ready to be used; no additional installation procedure is required.

4. Getting started
4.1. Starting Eclipse
To start Eclipse double-click on the file e l p e e e(Microsoft Windows) or e l p e(Linux / Mac) in cis.x cis the directory where you unpacked Eclipse. The system will prompt you for a workspace. The workspace is the place in which you work. Select an empty directory and press the OK button.

11/5/12

Eclipse IDE Tutorial

Eclipse will start and show the Welcome page. Close the welcome page by pressing the X beside Welcome.

After you closed the welcome screen you should see a screen similar to the following.

www.vogella.com/articles/Eclipse/article.html

5/46

11/5/12

Eclipse IDE Tutorial

4.2. Appearance
The appearance of Eclipse can be changed. By default Eclipse ships with a few themes but you can also extend Eclipse with new themes. To change the appearance, select from the menu Window Preferences General Appearance. The Theme selection allows you to change the appearance of your Eclipse IDE. Disabling the animations will make your Eclipse run faster.

Please note that you need to restart Eclipse to apply a new styling correctly. You can also install new themes. The appendix of this tutorial list popular themes. Bulk User Creation Active Directory Bulk User, MailBox Creation, CSV Import & Modification w w w .ManageEngi ne.com/ADManagerPlus 2013 Dodge Challenger America's Authentic Muscle Car Now Has Improved Handling. Get A Quote. w w w .Dodge.com/Challenger Nurse Training Program 2012 Nurse Training Program Enter Zip & Apply Now! w w w .Fi ndAnyCollege.com

5. Eclipse user interface overview


Eclipse provides Perspectives, Views and Editors. Views and Editors are grouped into Perspectives.

5.1. Workspace
The workspace is the physical location (file path) you are working in. Your projects, source files, images and other artifacts can be stored and saved in your workspace but you can also refer to external resources, e.g. projects, in your workspace.
www.vogella.com/articles/Eclipse/article.html 6/46

You can choose the workspace during startup of Eclipse or via the menu (File Switch Workspace Others) .

5.2. Eclipse projects


An Eclipse project contains source, configuration and binary files related to a certain task and groups them into buildable and reusable components. An Eclipse project can have certain natures assigned to it, e.g. it can be a Java project. Natures for a project are defined via the . r j c file in the project directory. poet

5.3. Parts
Parts are user interface components which allow you to navigate and modify data. Parts are typically divided into Views and Editors.

The distinction into Views and Editors is primarily not based on technical differences, but on a different concept of using and arranging these Parts. A View is typically used to work on a set of data, which might be a hierarchical structure. If data is changed via the View, this change is typically directly applied to the underlying data structure. A View sometimes allows us to open an Editor for a selected set of the data. An example for a View is the Java Package Explorer, which allow you browse the files of Eclipse Projects. If you choose to change data in the Package Explorer, e.g. if you rename a file, the file name is directly changed on the file system. Editors are typically used to modify a single data element, e.g. a file or a data object. To apply the changes made in an editor to the data structure, the user has to explicitly save the editor content. Editors were traditionally placed in a certain area, called the editor area. Until Eclipse 4 this was a hard limitation, it was not possible to move an Editor out of this area; Eclipse 4 allows the user to place Editors at any position in a Perspective or even outside a Perspective. For example the Java Editor is used to modify Java source files. Changes to the source file are applied once the user selects the Save command.

5.4. Perspective
A Perspective is a visual container for a set of Parts. The Eclipse IDE uses Perspectives to arrange Parts for different development tasks. You can switch Perspectives in your Eclipse IDE via the Window Open Perspective Other menu entry. The main perspectives used in the Eclipse IDE are Java perspective for Java development and the Debug perspective for debugging Java applications.

11/5/12

Eclipse IDE Tutorial

You can change the layout and content within a Perspective by opening or closing Parts and by rearranging them. To open a new Part in your current Perspective use the Window Show View Other menu entry. The following Show View dialog allows you to search for certain Parts.

In cases you want to reset your current perspective to its default, you can use the Window Reset Perspective menu entry. You can save your Perspective via Window Save Perspective As....

www.vogella.com/articles/Eclipse/article.html

8/46

11/5/12

Eclipse IDE Tutorial

6. Eclipse Java perspective


6.1. Toolbar
The application toolbar contains actions which you typically perform, e.g. creating Java resources or running Java projects. It also allow to switch between opened perspectives.

6.2. Useful Views


The Eclipse perspective contains useful Views which allow you to work with your Java project. The Package Explorer View allows you to browser the structure your projects and to open files via doubleclick. It also be used to change the structure of your project, e.g. you can rename files or move files or folder via drag and drop. A right mouse click on a file or folder shows you the available options.

www.vogella.com/articles/Eclipse/article.html

9/46

The Outline View shows the structure of the currently selected Java source file.

6.3. Opening a class


You can navigate between the classes in your project via the Package Explorer View. In addition you can open any class via positioning the cursor on the class in an editor and pressing F3. Alternatively, you can press Ctrl+Shift+T. This will show a dialog in which you can enter the class name to open it.

11/5/12

Eclipse IDE Tutorial

7. Create your first Java program


The following describes how to create a minimal Java program using Eclipse. It is tradition in the programming world to create a small program which writes "Hello World" to the console. We will adapt this tradition and will write "Hello Eclipse!" to the console.

7.1. Create project


Select from the menu File New Java project. Enter d . o e l . c i s . d . i s as the evglaelpeiefrt project name. Select the Create separate folders for sources and class files flag.

Press the Finish button to create the project. A new project is created and displayed as a folder. Open the d . o e l . c i s . d . i s folder and explore the content of this folder. evglaelpeiefrt
www.vogella.com/articles/Eclipse/article.html 11/46

11/5/12

Eclipse IDE Tutorial

7.2. Create package


In the following step you will create a new p c a e A good convention is to use the same name for the akg. top level package and the project. To create the d . o e l . c i s . d . i s package, select the folder s c right click on it and evglaelpeiefrt r, select New Package.

Enter the name of your new package in the dialog and press the Finish button.

7.3. Create Java class


Create a Java class. Right click on your package and select New Class.

Enter M F r t l s as the class name and select the public static void main (String[] args) flag. yisCas

www.vogella.com/articles/Eclipse/article.html

12/46

11/5/12

Eclipse IDE Tutorial

Press the Finish button. This creates a new file and opens the Editor for Java source files. Change the class to the following example.
p c a e d.oel.cis.d.is; akg evglaelpeiefrt p b i c a s MFrtls { ulc ls yisCas p b i s a i v i mi(tig]ag){ u l c t t c o d anSrn[ rs Sse.u.rnl(HloElpe"; ytmotpitn"el cis!) } }

7.4. Run your project in Eclipse


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

www.vogella.com/articles/Eclipse/article.html

13/46

11/5/12

Eclipse IDE Tutorial

Eclipse will run your Java program. You should see the output in the Console View.

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

8. Run Java program outside Eclipse


8.1. Create jar file
To run your Java program outside of Eclipse you need to export it as a j rfile. A j rfile is the standard a a distribution format for Java applications. Select your project, right click on it and select E p r . xot

www.vogella.com/articles/Eclipse/article.html

14/46

11/5/12

Eclipse IDE Tutorial

Select JAR file, select next. Select your project and maintain the export destination and a name for the jar file. I named it m p o r m j r yrga.a.

Press finish. This creates a jar file in your selected output directory. Congratulations! You created your first Java project, a package, a Java class and you ran this program inside Eclipse.

8.2. Run your program outside Eclipse


Open a command shell, e.g. under Microsoft Windows select Start Run and type c dand press enter. m This should open a console. Switch to your output directory, by typing c p t . For example if your jar is located in c \ e ptype c d ah :tm d c\ep :tm. firstjava_export To run this program you need to include the jar file in your c a s a h The c a s a h lspt. lspt defines what Java classes are available to the Java runtime. You can add a j rfile to the classpath with a the - a option. jr
www.vogella.com/articles/Eclipse/article.html 15/46

11/5/12

Eclipse IDE Tutorial


jv -lspt mpormjrd.oel.cis.d.is.yisCas aa casah yrga.a evglaelpeiefrtMFrtls

If you type the command from above and are in the correct directory you should see the "Hello Eclipse!" output on the console.

9. Exporting and importing projects


9.1. Exporting projects
You can export and import Eclipse projects in case you want to share your projects with someone else or want to use projects from someone else. To export Eclipse projects, select File Export General Archive File and select the projects you want to export.

www.vogella.com/articles/Eclipse/article.html

16/46

11/5/12

Eclipse IDE Tutorial

9.2. Importing projects


To import projects, select File Import Existing Projects into Workspace. You can directly import from your zip file or import projects.

www.vogella.com/articles/Eclipse/article.html

17/46

11/5/12

Eclipse IDE Tutorial

10. Content Assist, Quick Fix and Class Navigation


10.1. Content assist
The content assistant allows you to get input help in an editor. It can be invoked by pressing Ctrl+Space For example type s s in the editor of a Java source file and then press Ctrl+Space. This will replace yo s s with S s e . u . r n l ( " . yo ytmotpitn") If you have a reference to an object, for example the object p r o of the type P r o and need to see esn esn it's methods, type p r o .and press Ctrl+Space. esn

www.vogella.com/articles/Eclipse/article.html

18/46

11/5/12

Eclipse IDE Tutorial

10.2. Quick Fix


Whenever Eclipse detects a problem, it will underline the problematic text in the editor. Select the underlined text and press Ctrl+1 to see proposals how to solve this problem. For example type m B o e n = t u ;If myBoolean is not yet defined, Eclipse will highlight it as an yola re error. Select the variable and press Ctrl+1, Eclipse will suggest creating a field or local variable.

Quick Fix is extremely powerful. It allows you to create new local variables and fields as well as new methods and new classes. I can put try-catch statements around your exceptions. It can assign a statement to a variable and much more.

11. Opening a class


You can navigate between the classes in your project via the Package Explorer View. In addition you can open any class via positioning the cursor on the class in an editor and pressing F3. Alternatively, you can press Ctrl+Shift+T. This will show a dialog in which you can enter the class name to open it.

12. Generating code


Eclipse has several possibilities to generate code for you. This can save significant time during development.
www.vogella.com/articles/Eclipse/article.html 19/46

For example Eclipse can override methods from superclasses and generate the t S r n ( , otig) h s c d ( and e u l ( methods. It can also generate getter and setter methods for attributes of ahoe) qas) your Java class. You can find these options in the Source menu.

To test the source generation, create the following class in your d . o e l . c i s . d . i s evglaelpeiefrt project.
p c a e d.oel.cis.d.is; akg evglaelpeiefrt p b i c a s Pro { ulc ls esn p i a e Srn frtae rvt tig isNm; p i a e Srn lsNm; rvt tig atae }

Select Source Generate Constructor from Fields, mark both fields and press "Ok".

11/5/12

Eclipse IDE Tutorial

Select Source Generate Getter and Setter, select again both your fields and press the "Ok" button. Select Source Generate toString(), mark again both fields and press "Ok". You created the following class:
p c a e d.oel.cis.d.is; akg evglaelpeiefrt p b i c a s Pro { ulc ls esn p i a e Srn frtae rvt tig isNm; p i a e Srn lsNm; rvt tig atae p b i Pro(tigfrtae Srn lsNm){ u l c esnSrn isNm, tig atae spr) u e (; t i .isNm =frtae h s frtae isNm; t i .atae=lsNm; h s lsNm atae } p b i Srn gtisNm( { u l c tig eFrtae) r t r frtae e u n isNm; } p b i v i stisNm(tigfrtae { u l c o d eFrtaeSrn isNm) t i .isNm =frtae h s frtae isNm; } p b i Srn gtatae){ u l c tig eLsNm( r t r lsNm; e u n atae } p b i v i stataeSrn lsNm){ u l c o d eLsNm(tig atae t i .atae=lsNm; h s lsNm atae } @vrie Oerd p b i Srn tSrn( { u l c tig otig) r t r "esn[isNm= +frtae+" lsNm= +lsNm e u n Pro frtae" isNm , atae" atae +"" ]; } }

13. Refactoring
13.1. Refactoring
Refactoring is the process of restructuring the code without changing his behavior. For example renaming a Java class or method is a refactoring activity.
www.vogella.com/articles/Eclipse/article.html 21/46

11/5/12

Eclipse IDE Tutorial

13.2. Refactoring in Eclipse


Eclipse supports several refactoring activities, for example renaming or moving. For example you can select your class, right click on it and select Refactor Rename to rename your class or method. Eclipse will make sure that all calls in your Workspace to your your class or method will also be renamed. The following shows a screenshot for calling the Rename refactoring on a class.

14. Exercise:Refactoring
14.1. Preparation
For the next examples change the code of your M F r t l s class to the following. yisCas
p c a e d.oel.cis.d.is; akg evglaelpeiefrt p b i c a s MFrtls { ulc ls yisCas p b i s a i v i mi(tig]ag){ u l c t t c o d anSrn[ rs Sse.u.rnl(HloElpe"; ytmotpitn"el cis!) i t sm=0 n u ; f r ( n i=0 i< 10 i+ { o it ; = 0; +) sm+ i u = ; } Sse.u.rnl(u) ytmotpitnsm; } }

14.2. Perform refactoring


A useful refactoring is to mark code and create a method from the selected code. For this mark the coding of the "for" loop, right click and select Refactoring Extract Method. Use "calculateSum" as name of the new method.

www.vogella.com/articles/Eclipse/article.html

22/46

11/5/12

Eclipse IDE Tutorial

The resulting class should look like the following.


p c a e d.oel.cis.d.is; akg evglaelpeiefrt p b i c a s MFrtls { ulc ls yisCas p b i s a i v i mi(tig]ag){ u l c t t c o d anSrn[ rs Sse.u.rnl(HloElpe"; ytmotpitn"el cis!) i t sm=0 n u ; sm=cluaeu(u) u acltSmsm; Sse.u.rnl(u) ytmotpitnsm; } p i a e s a i i t cluaeu( n sm { rvt ttc n acltSmi t u) f r ( n i=0 i< 10 i+ { o it ; = 0; +) sm+ i u = ; } r t r sm e u n u; } }

You can also extract strings and create constants from them. Mark for this example "Hello Eclipse!", right click on it and select Refactor Extract Constant. Name your new constant "HELLO".

The resulting class should look like the following.


p c a e d.oel.cis.d.is; akg evglaelpeiefrt

www.vogella.com/articles/Eclipse/article.html

23/46

11/5/12

Eclipse IDE Tutorial


p b i c a s MFrtls { ulc ls yisCas p i a e s a i f n l Srn HLO="el Elpe" rvt ttc ia tig EL Hlo cis!; p b i s a i v i mi(tig]ag){ u l c t t c o d anSrn[ rs Sse.u.rnl(EL) ytmotpitnHLO; i t sm=0 n u ; sm=cluaeu(u) u acltSmsm; Sse.u.rnl(u) ytmotpitnsm; } p i a e s a i i t cluaeu( n sm { rvt ttc n acltSmi t u) f r ( n i=0 i< 10 i+ { o it ; = 0; +) sm+ i u = ; } r t r sm e u n u; } }

Eclipse has much more refactorings, in most cases you should get an idea of the performed action by the naming of the refactoring operation.

15. Eclipse Shortcuts


Eclipse provides a lot of shortcuts to work efficiently with the IDE. For a list of the most important Eclipse shortcuts please see Eclipse Shortcuts

16. Using project dependencies


You can define in Eclipse that a project uses another project. For this select your project, right mouse click on it and select Properties. Select Java Build Path and the Projects tab.

If you add a project to another project, you can use its classes.

17. Using jars (libraries)


17.1. Adding a library (.jar) to your project
The following describes how to add Java libraries to your project. Java libraries are distributed via "jar" files. It assumes that you have a jar file available; if not feel free to skip this step.
www.vogella.com/articles/Eclipse/article.html 24/46

11/5/12

Eclipse IDE Tutorial

Create a new Java project d . o e l . c i s . d . a s Then, create a new folder called l b evglaelpeiejr. i, by right clicking on your project and selecting New Folder.

From the menu select File Import General File System. Select your jar and select the l bfolder i as target. Alternatively, just copy and paste your j rfile into the l bfolder. a i Right click on your project and select Properties. Under Java Build Path Libraries select the Add JARs button. The following example shows how the result would look like, if the j n t 4 4 j rfile had been added to ui-..a the project.

Afterwards you can use the classes contained in the j rfile in your Java source code. a

17.2. Attach source code to a Java library


As said earlier you can open any class via positioning the cursor on the class in an editor and pressing F3. Alternatively, you can press Ctrl+Shift+T. This will show a dialog in which you can enter the class name to open it. If the source code is not available, the editor will show the decompiled bytecode of that class. This happens if you open a class from Java library and the source for this .jar file is not available. The same happens if you open a class from the standard Java library without attaching the source code to it.
www.vogella.com/articles/Eclipse/article.html 25/46

11/5/12

Eclipse IDE Tutorial

To browse the source of a type contained in a library (i.e. .jar file), you can attach a source archive or source folder to that library. Afterwards the editor will show the source instead of the bytecode. In addition setting the source attachment allows debugging this source code. The Source Attachment dialog can be reached in the Java Build Path page of a project. To open this page, right click on a project Properties Java Build Path. On the Libraries tab, expand the library's node, select the Source attachment attribute and press the Edit button. In the Location path field, enter the path of an archive or a folder containing the source. The following shows this for the standard Java library. If you have the Java Development Kit (JDK) installed, you should find the source in the JDK installation folder. The file is typically called s c z p r.i.

17.3. Add the Javadoc for a jar


It is also possible to add Javadoc to a library which you use. Download the Javadoc of the jar and put it somewhere in your filesystem. Open the Java Build Path page of a project via Right click on a project Properties Java Build Path. On the Libraries tab expand the library's node, select the J v d c l c t o attribute and press the Edit aao oain button. Enter the location to the file which contains the Javadoc.

www.vogella.com/articles/Eclipse/article.html

26/46

11/5/12

Eclipse IDE Tutorial

18. Updates and Installation of Plug-ins


18.1. Eclipse Update Manager
Eclipse contains a software components called Update Manager which allows you to install and update software components. Installable software components are called f a u e and consists of p u - n . etrs lgis To update your Eclipse installation, select Help Check for Updates. The system will search for updates for the already installed software components. If it finds updated components, it will ask you to approve the update. To install a new functionality, select Help Install New Software.

www.vogella.com/articles/Eclipse/article.html

27/46

11/5/12

Eclipse IDE Tutorial

From the "Work with" list, select an URL from which you would like to install. To add a new update site, press Add and enter the new URL as well as a name for the new update site. Sometimes you have to uncheck the Group items by category checkbox because not all available plug-ins are categorized. If they are not categorized, they will not be displayed, unless the grouping is disabled.

18.2. Eclipse Marketplace


Eclipse also contains a client which allows installing software components from the Eclipse Marketplace. The advantage of this client is that you can search for components, discover popular extensions and see descriptions and ratings. Compared to the update manager you do not have to know the URL for the update site. Not all Eclipse distributions contain the Market Client by default. You may need to install the Market before you can use it.

www.vogella.com/articles/Eclipse/article.html

28/46

11/5/12

Eclipse IDE Tutorial

To open the Eclipse Marketplace select Help Eclipse Marketplace.

www.vogella.com/articles/Eclipse/article.html

29/46

11/5/12

Eclipse IDE Tutorial

You can use the Find box to search for components. Pressing the Install button will start the installation process.

18.3. Restarting Eclipse


After an update or an installation of a new software component you should restart Eclipse to make sure that the changes are applied.

19. Advanced Eclipse Update manager options


19.1. Manual installation of plug-ins (dropins folder)
If youre using a plug-in for which no Update Site is available, you can use the d o i sfolder in your rpn Eclipse installation directory. Plug-ins are typically distributed as j rfiles. To add a plug-in to your Eclipse installation, put the plug-in a .jar file into the Eclipse d o i sfolder and restart Eclipse. Eclipse should detect the new plug-in and install rpn it for you.

19.2. Exporting and importing the installed components


Eclipse 4.2 allows to export a file which describes the installed Eclipse components. During the export the user can select which components should be included into this description file.

www.vogella.com/articles/Eclipse/article.html

30/46

11/5/12

Eclipse IDE Tutorial

Other users can import this description file into their Eclipse installation and install the described components. This way Eclipse installation can be kept in sync with each other. To export a description file, select File Export Install Installed Software Items to File and select the components which should be included into your description file.

To install selected components of this file in another Eclipse Installation, open it with File Import Install Install Software Items from File and follow the wizard.

19.3. Installing features via the command line


Eclipse p2 has a components called director which allow to install new features via the command line. For example the following will install the components Egit, Mylyn and EMF into an Eclipse instance.
elpeelpe\ cis/cis -plcto ogelpeeunxp.ietr\ apiain r.cis.qio.2drco -opah\ nSls -eoioy\ rpstr ht:/onodelpeogrlae/uo\ tp/dwla.cis.r/eessjn -ntlIs\ isalU ogelpeei.etr.ru, r.cis.gtfauegop\ ogelpeji.etr.ru, r.cis.gtfauegop\ ogelpeefskfauegop\ r.cis.m.d.etr.ru, ogelpemlnfauefauegop\ r.cis.yy_etr.etr.ru, ogelpewtxlu.etr.etr.ru r.cis.s.m_ifauefauegop

20. Eclipse Java development preferences


20.1. Overview

www.vogella.com/articles/Eclipse/article.html

31/46

11/5/12

Eclipse IDE Tutorial

The behavior of the Eclipse IDE can be controlled via the Preference settings. Select Window Preferences to open the preference settings dialog. You can use the filter box to search for specific settings. Correctly configuring Eclipse to your need can largely improve your productivity in using the Eclipse IDE.

20.2. Automatic placement of semicolon


Eclipse can make typing more efficient by placing semicolons at the correct position in your source code. In the Preference setting select Java Editor Typing. In the Automatically insert at correct position selection enable the Semicolons checkbox. You can now type a semicolon in the middle of your code and Eclipse will position it at the end of the current statement.

20.3. Auto escape text pasted into Strings


Eclipse allows to escape automatically text if they are pasted into a String literal. For example you can copy HTML code and paste it into a String in your Java source. Eclipse would escape the text automatically for you. Activate this setting via Windows Preferences Java Editor Typing In string literals Escape text when pasting into string literals Now can you paste text which requires that it is escaped. The following code snippets show an example.
Srn s="ahe="uoil.tl"Ttras/>; tig < rf\ttrashm\>uoil<a"

Srn s="AT HR" tig PSE EE;

www.vogella.com/articles/Eclipse/article.html

32/46

11/5/12

Eclipse IDE Tutorial

20.4. Bracket highlighting


You can configure Eclipse so that the matching brackets of a code block are highlighted in the source code editor.

Before the change you would not see the enclosing brackets afterwards they will be slightly highlighted. This helps to see in which block you are.

20.5. Activate Save Actions


Eclipse can format your source code and organize your import statements automatically during a save operation. This is useful as the Save shortcut (Ctrl+S) is easy to reach. You can find this setting under Java Editor Save Actions.

www.vogella.com/articles/Eclipse/article.html

33/46

11/5/12

Eclipse IDE Tutorial

Import statements will only be automatically imported, if Eclipse finds only one valid import. If Eclipse determines more than one valid import, it will not add import statements automatically.

20.6. Filter import statements


The Save Actions can automatically add required import statements to your source code if there is only one possible import. Alternatively or if there are several possible imports, you can use the Organize Imports shortcut (Ctrl+Shift+)). If there are several alternatives, Eclipse suggests all available packages and the user has to select the right one. To following shows the available packages for the L s class in the Organize Imports dialog. it

If you never use certain packages, for example AWT or Swing, you can exclude these packages from
www.vogella.com/articles/Eclipse/article.html 34/46

11/5/12

Eclipse IDE Tutorial

Eclipse via the Windows Preferences Java Appearance Type Filters setting. Press Add packages to add a specific package or Add to use wildcards. The following will exclude all AWT packages from import.

Please note that Eclipse shows (in its default configuration) only the packages which are used in the current workspace. If you want to exclude standard Java packages, you have to create at least one Java project.

20.7. Completion overwrites and insert guessed method arguments


Eclipse can override existing method calls, in case you trigger a code completion in an existing statement. Eclipse can also try to guess the correct actual parameters for a method call.

www.vogella.com/articles/Eclipse/article.html

35/46

11/5/12

Eclipse IDE Tutorial

With the first setting you can override methods in the middle of a statement via Ctrl+Space.

Without this setting you would get the following result, which results in a syntax error.

With this setting you get the following result.

21. More on preferences


21.1. Launch Configuration
Eclipse allows to start an application via the Run button in the menu or via the CTRL+F11 shortcut. By default Eclipse will determine if the currently selected file is executable and try to start that. If is sometimes confusing, you can configure the Eclipse IDE to start always the last started program. To configure that select Window Preferences Run/Debug Launching and define that always the previous launched application should be launched.

www.vogella.com/articles/Eclipse/article.html

36/46

11/5/12

Eclipse IDE Tutorial

21.2. Configuring the editors for a file extension


The Editors which are available to open a file can be configured via Window Preferences General Editors File Associations. The Default button in this preference dialog allows to set the default editor for a certain file extension, e.g. this is the editor which will be used by default if you open a new file with this extension. The other configured Editors can be selected, if you right mouse click on a file and select "Open With". Eclipse will remember the last E i o you used to open a file and use this E i o again the next time dtr dtr you open the file.

21.3. Export / Import Preferences


You can export your preference settings from one workspace via File Export General Preferences. Similarly you can import them again into another workspace.

22. Using and configuring template


22.1. Templates
If you have to frequently type the same code / part of the document, you can create templates which can be activated via autocomplete (Ctrl + Space). For example, assume that you are frequently creating p b i v i n m ( { methods. You could ulc od ae)} define a template which creates the method body for you. To create a template for this, select the menu Window Preferences Java Editor Templates.

www.vogella.com/articles/Eclipse/article.html

37/46

11/5/12

Eclipse IDE Tutorial

Press New. Create the template shown in the following screenshot.

$ c r o }indicates that the cursor should be placed at this position after applying the template. {usr In this example the name "npm" is your keyword. Now every time you type "npm" in the Java editor and press Ctrl+Space the system will allow you to replace your keyword with your template.

22.2. Code Templates


Eclipse generates lots of source code automatically. For example, in several cases comments are added to the source code. Select Window Preferences Java Code Style Code Templates to change the code generation templates.

www.vogella.com/articles/Eclipse/article.html

38/46

11/5/12

Eclipse IDE Tutorial

In the code tree you have the templates. Select for example Code Method Body and press "Edit" to edit this template and to remove the "todo" comment.

23. Organizing your workspace


23.1. Overview
The Eclipse IDE allows you to organize your project into workspaces so that you can hide certain resources. You can also place markers in the code which you can later access via the Task View.

23.2. Working Sets


You will create more and more projects in your development career. Therefore the data in your workspace grows and it is hard to find the right information. You can use working sets to organize your displayed projects / data. To set up your working set select the Package Explorer open the drop-down menu Select Working Set...

Press New on the following dialog to create a working set.

www.vogella.com/articles/Eclipse/article.html

39/46

11/5/12

Eclipse IDE Tutorial

On the next dialog select Resource, press the Next button and select the projects you would like to see and give it a name.

www.vogella.com/articles/Eclipse/article.html

40/46

11/5/12

Eclipse IDE Tutorial

You can now easily display only the files you want to see.

23.3. Task Management


You can use / T D comments in your code to add task reminders. / OO This indicates a task for Eclipse. You find those in the Task View of Eclipse. Via double-clicking on the task you can navigate to the corresponding code. You can open this View via Window Show View Tasks. For example, add a TODO to your M F r t l s class to see it in the Tasks View. yisCas
p c a e d.oel.cis.d.is; akg evglaelpeiefrt p b i c a s MFrtls { ulc ls yisCas p i a e s a i f n l Srn HLO="el Elpe" rvt ttc ia tig EL Hlo cis!; p b i s a i v i mi(tig]ag){ u l c t t c o d anSrn[ rs / TD Poieue itrae / OO rvd sr nefc Sse.u.rnl(EL) ytmotpitnHLO; i t sm=0 n u ; sm=cluaeu(u) u acltSmsm; Sse.u.rnl(u) ytmotpitnsm; } p i a e s a i i t cluaeu( n sm { rvt ttc n acltSmi t u) f r ( n i=0 i< 10 i+ { o it ; = 0; +) sm+ i u = ; } r t r sm e u n u; } }

Close the editor for the M F r t l s class. If you now double-click on the tasks, the Java editor opens yisCas
www.vogella.com/articles/Eclipse/article.html 41/46

11/5/12

Eclipse IDE Tutorial

again and the TODO comment is selected.

A more advanced tasks management is available with the Mylyn plug-in.

24. Eclipse command line configuration


24.1. Eclipse startup parameters
Eclipse allows that certain behavior is configured via startup parameters. This requires that you start Eclipse from the command line or that you configure your launcher links so that these parameters are considered. The following parameters are relevant for the Workspace. Table 1. Workspace Startup Parameters
Parameter -data workspace_path -showLocation Description Predefine the Eclipse workspace. Configures Eclipse so that is shows the current workspace directory in the title of Eclipse.

For example if you want to start Eclipse under Microsoft Windows using the c \ e pdirectory as :tm workspace you can use the following command from the command line.
c\cis.x -aa":tm" :elpeee dt c\ep

Depending on your platform you may have to put the path name into double quotes.

24.2. Eclipse initialization file


Your Eclipse installation contains a file called e l p e i iwhich can be used to set parameters for your cis.n Eclipse IDE and your Java virtual machine. For example the - m parameter can be used to define how Xx large the Java heap size can can get. - m defines the initial heap size of the Java virtual machine. Xs The Eclipse specific runtime parameter are described under the following URL: http://help.eclipse.org/juno/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/misc/runtimeoptions.html

24.3. Default values for preference settings


You can specify default values for preferences via file which is typically called p u _ u t m z t o . n . lgcsoiainii This this file you can setup default values for preference settings. For example the following will setup a
www.vogella.com/articles/Eclipse/article.html 42/46

11/5/12

Eclipse IDE Tutorial

default type filter for the j v . w and j v x s i gpackage. aaat aa.wn


ogelpejtu/r.cis.d.itpfle.nbe=aaat*jvxsig* r.cis.d.iogelpejtu.yeitrealdjv.w.;aa.wn.;

You link to this file via your eclipse.ini file in your Eclipse installation directory. The following assumes that you created the p u _ u t m z t o . n file in the Eclipse installation directory. lgcsoiainii
-lgnutmzto puiCsoiain pui_utmzto.n lgncsoiainii -tru satp puisogelpeeunxluce_...2102-83jr lgn/r.cis.qio.anhr130v025211.a -luce.irr -anhrlbay puisogelpeeunxluce.t.iu.8_41120v025211 lgn/r.cis.qio.anhrgklnxx66_..0.2102-83 -rdc pout ogelpeepp c a e rppout r.cis.p. a k g .c.rdc -luce.ealAto -anhrdfutcin oeFl pnie -hwpah sosls ogelpepafr r.cis.ltom -luce.XaPrSz -anhrXMxemie 26 5m -luce.ealAto -anhrdfutcin oeFl pnie -mrs vag -og.eurdaaeso=. DsirqieJvVrin15 -hl.ueetknzrsadr Deplcn.oeie=tnad -XMxemie26 X:aPrSz=5m -m4m Xs0 -m52 Xx1m

Only the - l g n u t m z t o p u i _ u t m z t o . n parameter was added to the default puiCsoiain lgncsoiainii


e l p e i ifile. cis.n

To identify additional preference settings you can use the following approach: start a new workspace change the preference export all preferences search the key in the exported file Note that you need to remove the scope (e.g. /instance/) before copying it into the
p u _ u t m z t o . n file. lgcsoiainii

25. Eclipse online resources


25.1. Online documentations
The Eclipse help system is available from within your Eclipse installation as well as online. With your running Eclipse IDE you can access the online help via Help Help Contents. This will start a new window which shows you the help topics for your currently installed components.

www.vogella.com/articles/Eclipse/article.html

43/46

11/5/12

Eclipse IDE Tutorial

Online you find the online help under http://www.eclipse.org/documentation/. The online help is version dependent and contains the help for all Eclipse projects included in the selected release.

25.2. Webresources
The Eclipse homepage also contains a list of relevant resources about Eclipse and Eclipse programming. You find these resources under http://www.eclipse.org/resources/. You also find several Eclipse IDE relevant one tutorials on the following webpage: http://www.vogella.com/eclipse.html.

26. Reporting Eclipse bugs and asking questions


26.1. Asking (and answering) questions
Due to the complexity and extensibility of Eclipse you will need additional resources to help you resolve your specific problems. Fortunately the web contains several resources which can help you with your Eclipse problems. Currently the best places to ask questions are the Eclipse forums and Stack Overflow. The Eclipse forums can be found under the following URL: http://eclipse.org/forums Stack Overflow can be found under the following URL: http://stackoverflow.com The Eclipse forums offer several topic specific forums in which you can post and answer questions. To post questions in the Eclipse forums you need a valid user account in the Eclipse bugtracker. The advantage of the Eclipse forums is that, depending on the topic, Eclipse committers are also active there and might directly answer your question. Stack Overflow also requires a user account and its community is also very active. Stack Overflow does not have separate forums for specific questions. In Stack Overflow you tag your questions with the relevant keyword, e.g. Eclipse and people search for them or subscribe to them. Both places are excellent places to ask questions. If you ask a question it is in general good advice to be
www.vogella.com/articles/Eclipse/article.html 44/46

11/5/12

Eclipse IDE Tutorial

polite and to give a good error description as this motivates people to give you high quality answers.

26.2. Eclipse Bugs


Eclipse has a public bug tracker based on Bugzilla. Bugzilla is an Open Source project. This bugtracker can be found under https://bugs.eclipse.org/bugs/. Here you can search for existing bugs and review them. To participate actively in the Eclipse bugtracker you need to create a new account. This can be done by pressing the Open a New Account link.

Once you have a user account, you can login to the Eclipse bugtracker. This allows you to comment on existing bugs and report new ones.

27. Next steps


To learn how to debug Eclipse Java programs you can use Eclipse Debugging To learn Java Web development you can use with Servlet and JSP development . If you want to develop rich stand-alone Java clients you can use Eclipse RCP You can extend Eclipse with Eclipse Plug-ins . Good luck in your journey of learning Java!

28. Thank you


Please help me to support this article:

29. Questions and Discussion


Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article
www.vogella.com/articles/Eclipse/article.html 45/46

11/5/12

Eclipse IDE Tutorial

please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

30. Links and Literature


30.1. Source Code
Source Code of Examples

30.2. Eclipse Resources


30.2.1. Eclipse Eclipse.org Homepage 30.2.2. Eclipse additional themes Very flexible Eclipse Theme by Jeeeyul Dark Eclipse Theme by Roger Dudler

30.3. Eclipse Bug Tracker and Eclipse Forum


Eclipse Forum for asking questions and providing feedback. Eclipse Bug Tracker for reporting errors or feature requests.

30.4. vogella Resources


vogella Training Android and Eclipse Training from the vogella team Android Tutorial Introduction to Android Programming GWT Tutorial Program in Java and compile to JavaScript and HTML Eclipse RCP Tutorial Create native applications in Java JUnit Tutorial Test your application Git Tutorial Put everything you have under distributed version control system

www.vogella.com/articles/Eclipse/article.html

46/46

You might also like