New Java Project
From the New Wizard (accessible via File → New → Projects… or Ctrl+N or Quick Access), you can reach multiple wizards to create projects. The 3 main ones for Java development are.
- Maven project: Generates a Maven project, creating the pom.xml, the project structure. A page of the wizard allows you to select archetype (project templates/examples). It’s often useful to spend time considering the best archetype to use.
- Dynamic Web projects: This is actually the entry point for a Servlet-based project, usually packaged as a .war and deployed to an application server.
- Plain Java Project: A simple Java project, suitable for standalone Java libraries or applications. The generated project won’t include support for a build system or frameworks.
Once your Java Project is created, you can right-click on it to perform many operations, such as creating a new class, a new interface, a new JUnit test, or some specific classes (Beans, Jax-RS) for certain projects.
Java Project Settings
On a project, try the Properties context menu (Alt+Enter). From there you can tweak many things, such as error reporting, classpath, Java compliance, and more.
Here are the main shortcuts for Java navigation.
||Show Javadoc for selected element
||Go to declaration
||Show type hierarchy
||Open a type (class, interface, enum)
||Go to… (simply Ctrl+hover shows multiple suggestions when useful)
||Open Project Properties
As always, many more shortcuts are available in the General → Keys page of the Preferences.
Automatic Build and Error Reporting
By default, Eclipse is configured to Build Automatically (in the Project menu, Build Automatically is ticked). This enables various analyzers on the project and reports errors and problems directly in the code.
You can at any time force a full build by going to Project → menu (Ctrl+B), or typing “Build All” in Quick-Assist/Ctrl+3. It may be useful to sometimes run Project → Clean… to clean the various caches and work folders if you suspect those are causing some trouble.
Eclipse usually provides resolutions for the errors it reports. Those are named Quick-Fix and you can view them by hitting Ctrl+1 when selection is on a problem. The severity of most problems can be configured in the Project Properties.
Some Eclipse extensions such as the FindBugs plugin can provide additional error reporting to improve the quality of your code.
At any time, the view (Window→ Show View/Alt+Shift+Q Q or “Problems” in Quick-Access/Ctrl+3) lists all detected problems in all your projects. Try to fix all issues reported in that view for good productivity and good code quality.
||(on an error) Available quick fix
Refactoring and Code Formatting
Eclipse comes with a lot of refactoring and other advanced editing operations. All are accessible by right-click under the Source (Shift+Alt+S) and Refactoring (Shift+Alt+R) context menus. The available operations depend on the current selection. The most common refactorings also have direct shortcuts, and some of them for the current selection are directly shown with Quick-Assist/Ctrl+2. As usual, all those operations can also be reached simply by Quick-Access/Ctrl+3. Examples of the most useful refactorings include: Rename, Convert Local Variable to Field, Extract Method, Extract Interface...
||Show advanced editing operations for current selection
||Show refactorings for current selection
||Quick-Assist: most usual refactorings for current selection
||Comment selected lines
||Rename (variable, field, method, class…)
The Run As context-menu on a Java element is populated by what seems to be the best Run Configurations for your current selection. Just select one and it will run it.
It’s often useful to customize the pre-existing Run Configurations or even create your own. Customizing a Run Configuration allows to more easily set additional System Properties, Environment Variables, Java settings, etc, that will be used at runtime. Once you have a Run Configuration ready, it is stored and can be run as many times as you want.
The entry points to tweak Run Configurations are.
- Run As → Run Configurations… context-menu
- Run Configurations… under the toolbar button
- Run → Run Configurations… menu
- Run Configurations… in Quick Access/Ctrl+3.
An interesting Run Configuration is accessible via Run As → Run on Server context menu on Java Web projects. Once you have configured a server in the Servers view, you can use it to easily re-deploy your application to the server, usually without requiring for a restart. Note that in the server properties (accessible via context menu from Server view), you can set the Publishing to Automatically deploy when resource changed so your web project associated to this server will get automatically updated on change without requiring you even to use Run As → Run on server....
Eclipse also has Run Configurations for specific kinds of project, such as Maven, Applet, OSGi, and Gradle. Extensions for frameworks often provide some specific Run Configurations, so if you work on some framework that’s not supported out-of-the-box, make sure you spend some time trying to find an extension for it on Marketplace or on the web.
Debugging in Eclipse is just another flavor of running the application as documented above. The difference is that we now talk about Debug Configurations which are just like Run Configurations, but with debug enabled.
An interesting Debug Configuration provides the ability to connect debugger to an external Java application. To do so, make sure your application to debug is started with the debug flags, usually -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n
or using some more specific flags in some context, then get to the Debug Configurationsmenu (click on toolbar button menu, or with Quick Access/Ctrl+3), and create a new Remote Java Application debug configuration. Set the debug port and the related workspace project, then you’ll be able to fully debug your external application.
Add breakpoints in your Java code by double-clicking on the column to the left of line numbers or using the right-click → context-menu.
Manage breakpoints (enablement, grouping...) from the view. A powerful feature is Conditional Breakpoints. On a breakpoint, do right-click →Properties to specify a condition to stop on a breakpoint.
When hitting a breakpoint
- the view shows threads and method call stack
- the view shows values of the variables for the currently selected stack frame
- the view allows to define complex expressions that will be evaluated and visible immediately for the selected frame
- The view allows you to write some “scratch” pieces of code to run, evaluate, inspect your custom code in the context of the currently selected stack frame.
- The context-menu (Shift+Alt+I) on a variable, field of expression show the value of the selection
- The context-menu on a variable, field or expressions adds it to the Expressions view for constant re-evaluation.
As usual, views can be opened via Window → Show View menu, Alt+Shift+Q Q shortcut, or using Quick-Access/Ctrl+3 and typing the view name. The context-menu operations are also available with Quick-Access/Ctrl+3.
Most of those views are visible by default in the Debug perspective, that will be recommended when Eclipse the debugger notices the target application is suspended by a breakpoint or an error.
|Double-click on left column
||Double-click on left column
||Add/remove breakpoint on selected line.
||Step over: go to next line
||Step return: go back to caller
||Resume: continue execution until next breakpoint
|Hovering on variable
||Hovering on variable
||Show variable value
||Inspect: show selected expression value
Eclipse comes with an integration with JUnit. You can create unit tests from the New Wizard under Java→JUnit category, or on the context-menu on a Java element or from Quick Access/Ctrl+3. Run those tests with right-click →Run As → on a JUnit test class. The test report will be shown in a dedicated view that shows a clear status of your tests, and allowing to do advanced filtering, to navigate inside your production code and to compare expected and actual results.
Running a JUnit test is a regular Run/Debug Configuration, so the steps mentioned above can be used to control execution and debugging of Unit Tests.
On Marketplace and other places on the web, you can find nice additions for Eclipse regarding testing. For example, support for TestNG, test coverage with EclEmma, automatic generation of tests and easier navigation between unit test and class under test with MoreUnit, or continuous test execution in background with Infinitest can be very useful.
When you’re done with your code and want to turn your project into a delivery, you can usually open the Export Wizard via the context-menu on a project. You can select multiple strategies for export. It’s up to you to decide which one is the best according to your project. The most common ones, depending on your project, are and WAR file.
Maven integration for Eclipse is provided out-of-the-box, it doesn’t require any addition.
To import Maven projects, use the wizard from the File → menu or from Quick-Access/Ctrl+3. Maven support in Eclipse will run various analysis, and may recommend you to install some extensions to better support your project.
Do NOT use the deprecated mvn eclipse:eclipse command.
Once your Maven project is imported, you can write code taking advantage of all Eclipse features, including incremental build and error reporting. If you do need to specifically run the Maven build, right-click on the Run As… → context-menu. A Maven build in Eclipse is a Run Configuration, so it can be tweaked or even debugged as mentioned earlier.
Eclipse has an extension for Gradle projects, which is called BuildShip. If your IDE doesn’t include Gradle support, you can install it from Marketplace.