With over 10 years of releases and countless extensions and plugins, Eclipse remains one of the most popular IDEs for developers working across several domains. Especially those in the Java ecosystem, where Eclipse provides a solid environment for developing, debugging and running applications. In this post, I’d like to share my favorite features in Eclipse that help me be more productive in my daily work as a Java developer. These features don’t require any plugin to be installed if the very first tip is followed.
1. Use Eclipse Oxygen
There are many improvements in the last release of Eclipse alone, and with the latest build released just a few days ago, the IDE supports Java 9 out of the box. Some of these improvements are:
- Showing the last returned value while debugging a Java program. This shows you the value returned by the last method after stepping through the program statements.
- Better Java 8 support, with numerous bug fixes for lambda expression type inference.
- A new Java index that significantly reduces tooling performance, such as when loading the type hierarchy of platform classes and interfaces.
- Support for JUnit 5.
You can find a list of Java tooling improvements in Oxygen here.
2. Switch editors using Ctrl+Tab
If you’re used to switching tabs in browsers and editors like Notepad++, then do yourself a favor and change the keybinding for switching between Java source files. Go to Window -> Preferences -> Keys, then search for “Next editor” and “Previous editor”, and override their bindings to Ctrl+Tab and Ctrl+Shift+Tab respectively (note: I’m using Windows).
3. Group related projects in working sets before choosing multiple workspaces
If you work on many different projects, at some point you may need to use multiple workspaces to separate them. But before reaching that level, you can just group related projects into a working set. This way you don’t have to switch workspaces or have two Eclipse windows using different workspaces. It also keeps your projects organized and accessible from within the same view. For example, I typically keep a working set for sample projects for quick experimentation, and then typically a working set for each group of related modules in a Maven project. One thing that you may need to do is change the Package Explorer view to view these working sets as shown below.
I often end up with many working sets containing projects I’m not using. In this case, I can simply close a working set by right-clicking on it from the Package Explorer, and selecting Close Project. This reduces memory consumption in the IDE and makes it as if these closed projects do not exist in your workspace anymore until you re-open them. Only when I have too many working sets, or I have projects that considerably differ from each other that I rarely switch between, then I separate them in different workspaces.
4. Set the “incremental” option in the search dialog
When you hit Ctrl+F to find text in a source file, check the Incremental checkbox in the search dialog to make the occurrence of the searched text appear as you type. This seemingly minor detail helps me avoid typing too many characters and then hitting return to find what I want.
5. Use navigation and search shortcuts
A few shortcuts to help understanding your code (using Windows). These are so useful that eventually they’ll easily become second nature:
- F3 or Ctrl+Left click: goes to declaration of element
- Ctrl+T: view type hierarchy and implementation methods
- Ctrl+Alt+H: view call hierarchy of selected element
- Ctrl+Shift+G: search workspace for all references to selected element
- Ctrl+Shift+T: search for a class, interface or enum
- Ctrl+Shift+R: search for a resource (e.g. text file)
6. Use the File Search feature
This is really helpful if you want to search files in your workspace for text. The search can be filtered by file type and scope of the search, such as searching only the selected project.
7. Use Ctrl+Space and Ctrl+1 for content assist and quick fixes
Ctrl+Space allows for auto-completion. It can also be used to override methods and generate getters /setters and constructors.
Ctrl+1 can be very handy in quick and smart fixes such as:
- assigning constructor parameters to new or existing class fields
- assigning a statement to a local variable
- renaming a variable, field or class, etc.
8. Use code generation actions
Alt+Shift+S can be used to quickly add common code:
- generating getters/setters and constructors
- overriding hashCode() and equals()
- overriding toString()
9. Ctrl+3 is your friend
As with any modern IDE, there are many keybindings to do all sorts of actions in Eclipse. But the most important is probably Ctrl+3 for Quick Access which is an entry point to virtually all actions you can do. I typically use it to open a view, but it can also be used to do refactoring, creating a new project, and lots of others.
10. Download the sources of libraries
If you’re using Maven, you can download the source code of your dependencies. There is preference under Window -> Preferences -> Maven that when selected automatically fetches the source artifacts of dependencies. You can also download sources manually by right-clicking on the dependency in the Maven Dependencies tree and selecting Maven -> Download Sources. Usually this also makes Javadoc comments available in your IDE when you hit F2, so you no longer need to browse it separately. There is similar way to do this in Gradle.
11. Use shortcuts to run, debug and inspect code
These again should become second nature while debugging:
- Ctrl+F11 to run the last application launched
- F11 to debug the last application launched
- F5 to step into
- F6 to step over, i.e. go to next line
- F7 to step return to caller
- F8 to resume until next breakpoint
- Ctrl+Shit+I to evaluate selected expression
- Use the Display view to write and execute code within the current debug context
12. Pinpoint program suspension with conditional breakpoints and watchpoints
Often you can make your program suspend on a line of code only when a certain condition is met. This reduces time spent on debugging, as long as you don’t overuse the feature (too many breakpoints, especially conditional ones, can make the program run slower in debug mode; in this case, you can either disable or delete unneeded breakpoints).
13. Save your run configurations for later re-use
I often need to build multi-module projects with different parameters, or run unit tests for a specific project, or configure some parameters for running a main class. Instead of switching to the command line, take the time to configure an appropriate run or debug configuration within the IDE. Ideally I never want to have a separate command line and everything should be done in the IDE, especially that it comes with all major build tools and SCM plugins already bundled.
14. Leverage code coverage support
In Eclipse Oxygen, the Eclipse EclEmma tool based on the JaCoCo code coverage library is integrated as part of the IDE, which allows you to have information on code coverage when running your unit tests. To run a program or unit test with coverage, right-click on the class to run and select Coverage As -> Java Application or JUnit Test.
15. For large workspaces with lots of dependent projects, disable Build Automatically
If you have lots of projects that depend on each other, the Build Automatically default behavior can be time-consuming because it would trigger an “internal” build upon saving. In this case, you can uncheck it from under the Project menu, which makes you in control of when to manually build your project. For example, after saving all needed modifications, the developer can manually do the internal build using Ctrl+B or using Project -> Build All, or do a full build from scratch using whatever build tool he or she is using.
And those were 15 habbits for improving productivity which I hope to be useful for Java developers using Eclipse! Of course many of them may depend on the developer’s preferences, and there are others that are not mentioned.Follow @MahmoudAnouti