Eclipse IDE Tutorial
Eclipse IDE Tutorial
Eclipse IDE Tutorial
Training Books
by Lars Vogel
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
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.
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.
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.
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.
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 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
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.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.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
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
www.vogella.com/articles/Eclipse/article.html
9/46
The Outline View shows the structure of the currently selected Java source file.
11/5/12
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
Enter the name of your new package in the dialog and press the Finish button.
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
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!) } }
www.vogella.com/articles/Eclipse/article.html
13/46
11/5/12
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.
www.vogella.com/articles/Eclipse/article.html
14/46
11/5/12
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.
11/5/12
If you type the command from above and are in the correct directory you should see the "Hello Eclipse!" output on the console.
www.vogella.com/articles/Eclipse/article.html
16/46
11/5/12
www.vogella.com/articles/Eclipse/article.html
17/46
11/5/12
www.vogella.com/articles/Eclipse/article.html
18/46
11/5/12
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.
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
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
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; } }
www.vogella.com/articles/Eclipse/article.html
22/46
11/5/12
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".
www.vogella.com/articles/Eclipse/article.html
23/46
11/5/12
Eclipse has much more refactorings, in most cases you should get an idea of the performed action by the naming of the refactoring operation.
If you add a project to another project, you can use its classes.
11/5/12
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
11/5/12
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.
www.vogella.com/articles/Eclipse/article.html
26/46
11/5/12
www.vogella.com/articles/Eclipse/article.html
27/46
11/5/12
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.
www.vogella.com/articles/Eclipse/article.html
28/46
11/5/12
www.vogella.com/articles/Eclipse/article.html
29/46
11/5/12
You can use the Find box to search for components. Pressing the Install button will start the installation process.
www.vogella.com/articles/Eclipse/article.html
30/46
11/5/12
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.
www.vogella.com/articles/Eclipse/article.html
31/46
11/5/12
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.
www.vogella.com/articles/Eclipse/article.html
32/46
11/5/12
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.
www.vogella.com/articles/Eclipse/article.html
33/46
11/5/12
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.
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 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.
www.vogella.com/articles/Eclipse/article.html
35/46
11/5/12
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.
www.vogella.com/articles/Eclipse/article.html
36/46
11/5/12
www.vogella.com/articles/Eclipse/article.html
37/46
11/5/12
$ 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.
www.vogella.com/articles/Eclipse/article.html
38/46
11/5/12
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.
www.vogella.com/articles/Eclipse/article.html
39/46
11/5/12
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
You can now easily display only the files you want to see.
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
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.
11/5/12
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
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
www.vogella.com/articles/Eclipse/article.html
43/46
11/5/12
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.
11/5/12
polite and to give a good error description as this motivates people to give you high quality answers.
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.
11/5/12
please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
www.vogella.com/articles/Eclipse/article.html
46/46