This Tutorial Covers Common Eclipse Features Such As Perspectives & Views, Importing/Exporting Programs, Refactoring, Quick Fix Etc. To Make Coding Easier:
In this Full Eclipse Training for All, we learned how to Create a Basic Java Project in Eclipse IDE in the previous tutorial.
In this tutorial, we will understand some of the basic features of Eclipse IDE which in turn, would make coding easier and thereby help us to manage projects in a better manner.
What You Will Learn:
Adding Libraries to Your Project
Here we have created a one-line project which will function perfectly by the default JRE system libraries added to the project while creation. However, when you work on actual projects they will be more complex and may require you to add libraries to support your work.
Specific libraries can be added to individual projects based on the requirement.
Follow the steps below to add a new library to your project:
#1) Right-click on your project folder and click on Build Path -> Configure Build Path. The Java build path window will be opened.
The Eclipse Java IDE will have default JRE libraries already added in your build path.
#2) Click on Libraries and then click on the button Add External Jars.
#3) Your local drive will be opened, navigate to the folder location where you have saved the respective jar, select the required jar and click Open.
#4) The Jar will appear in your libraries.
Click on Apply and close.
#5) If you check your project folder in the project explorer, then you will be able to see the jar there. You can now use any feature provided in this jar in your code.
Perspectives and Views
Perspectives are a collection of views and editors to work on similar types of projects. The one that we have been using above for simple Java projects is the default perspective or the Java perspective.
To open a specific perspective you can navigate to the Top Menu, Windows -> Perspective -> Open perspective -> other and the following window will open up.
Select the perspective that you want to open and click on open. As soon as you do that, the view of your Eclipse will change to the selected perspective and you will be able to see the same in the top corner.
You can right-click on the highlighted icon to customize, reset, or close the opened perspective.
From the above list, the commonly used perspectives include:
- Java default: This is used for writing Java code as we have seen in the previous section. If you have installed the Eclipse Java IDE, this will be your default perspective.
- Debug: This is used for debugging your code. We will explain debugging in detail in our upcoming tutorial.
- Git: This will be used if you want to use Git as a repository for your code, however, this may need some more plugins in Eclipse.
As stated already, Views are simply how all the features related to a perspective are displayed on the screen. You can adjust your views by dragging and dropping various sections of the views to the desired positions. To drag, simply click and hold on the sections title bar and drag it to a position of the desire and unclick to drop.
You can add more features to your view by going to Windows -> Show view, where a list of view items will be displayed to you, you can select something that is missing in the current view and as soon as you click it, the item will be added to the view.
The progress item selected from the list will be added as shown below:
Other Features and Terms That You Need to Know
Importing an Existing Project in Eclipse
If you already have a project and want to use the same project and code to it, then you need to import the project. To import a project Go to File -> Import, and the following wizard will open up.
Select the file system and click next. On the next page, you will get the option to browse for a project from your machine’s directory system, browse to your project location and select all or few files and click finish.
Exporting a Project
Just like importing, you can also export a project from Eclipse. To export a project right click on the project name and then click on export, and the following window will open up.
For Java projects, you can select one of the given options as per your need. You can also export a project as a file system by navigating in the general option.
While writing code, many times you will find yourself wanting to rename a file or a class. However, if you have used the same class in multiple places, then it may not be possible to rename it in all places. Indeed, Eclipse provides a solution to this problem.
Right-click on the file name, or class name or even the method name, or anything you want to rename and right-click on it, on right-click locate an option called “refactor”. In this refactor option, you will get a sub-option to rename, once you select that, the following window will be opened.
As shown above, there is an option to update references which have been checked, this will ensure that anywhere this value is used will be updated with the new name you give it. Similarly, there are few other options given to update while renaming something in your code.
In the refactor option, there is another option called move. This option enables you to move a file from one location to another, which in turn again allows you to update it at all places at which it is referenced.
Finding a Resource
When you have multiple projects or complex projects in your workspace and if you are looking for a specific file, then you cannot open every file and look for it. Eclipse makes this easier by a feature called Navigate.
Click on Navigate from the top menu and from there click on the open resource (CTRL+SHIFT+R), and the following window will be displayed.
As you can see, only by typing a part of the class name, the full class name is displayed. Here we have only one sample file, however, in case you have multiple files, then all of them will be displayed and you can click on the one you want from the result to open it.
Navigation also gives you the feature to Open Type which lets you search for a Java Type and Open Type in Hierarchy which again opens the Type with hierarchy. These two will give you results from the Build path along with your own Java classes.
This means that the JRE files that are added to your build path are nothing but helpful Java classes that have been standardized and can be reused by all developers. These two options will give you results based on your search from these Java files as well.
Eclipse gives us an option to generate getters and setters inside a file for the variables given in the file. This can be done by right-clicking anywhere inside the open file and then clicking on Source -> Generate getters and setters.
Similarly, from this source option, you can generate or remove comments, organize imports, generate constructors and a few more advanced options as well.
Content Assist is one of the strongest features that help developers. It helps you to complete the code just by entering a partial code in the editor.
For Example, if you enter System.out and then hit CTRL+space, the content sense will give you all the methods available for output like println(), print(), etc.
These are a few of the Eclipse features that make the task of a developer easier.
Quick Fix is a feature of the Eclipse where it highlights mistakes in the code you have written. Errors are highlighted with a red underline and cross whereas warnings are with a yellow line and cross.
On hovering on these errors and warnings, Eclipse also gives you ways to fix your code.
Similar to Quick Fix, Eclipse also offers the Hover functionality. Like in the Quick fix we hover on an error and see the possible solution if we hover on a method or any other code segment, Hover gives us the documentation associated with that segment.
If there is no documentation attached to it, Hover will simply provide the signature of the method/ class, etc. However, if there is documentation attached, Hover will display the details.
In this tutorial we have learned the following:
- Adding JAR to your project.
- A basic overview of Perspectives and Views.
- Few Eclipse features that will make coding easier.
Our upcoming tutorial will teach you all about debugging your code in Eclipse!!