Monday, October 31, 2016

Debugging Tomcat from IntelliJ IDEA

If you use IntelliJ IDEA 14, you have just a few simple steps to take to get up and running with your web applications. The first thing you need to do is set up IntelliJ to recognize your local Tomcat or other container installation. This is a one-time-only step you set it up once in your global IDE settings, then you can use the application server for any web application project. Next, set up each web application project to use your configured container. Finally, you just need to start your application from IntelliJ and place breakpoints where you’d like to debug your application.

To start, you need to configure Tomcat in IntelliJ’s list of application servers.

1. Open up IntelliJ’s IDE settings dialog. With a project open you can go to File ➪ Settings, or click the Settings icon in the toolbar, or press Ctrl + Alt + S. If you don’t have a project open, you can click the Configure button and then the Settings button.

2. In the left pane of the Settings dialog, click Application Servers under IDE Settings. Initially, you have no application servers configured.



3. Click the green plus icon to add a new application server. Click the browse button next to the Tomcat Home field to browse for and select the Tomcat home directory (for example, C:\Tomcat 8.0). Then click OK. IntelliJ should automatically detect your Tomcat version.


4. Click OK again to complete adding Tomcat to your list of application servers, and change the name if you want.

5. Click Apply to save the changes and OK to close the Settings dialog.

After you create a project and are ready to deploy it to Tomcat from IntelliJ, you need to add a Tomcat run/debug configuration to your project.

1. Click the run/debug configurations icon (a down arrow) on the toolbar, then click Edit Configurations.


2. In the dialog that appears, click the green plus icon, scroll to the bottom of the Add New Configuration menu, hover over Tomcat Server, and click Local. This creates a run/debug configuration for running your project against a local Tomcat.


3. If Tomcat 8.0 is the only application server you have added to IntelliJ, it is automatically selected as the application server this run/debug configuration will use. If you have other application servers configured, one of those might be selected, in which case you need to click the “Application server” drop-down and select Tomcat 8.0 instead.

4. Name the run configuration something meaningful.

5. You’ll probably see a warning that no artifacts are marked for deployment. Correcting this is simple. Click the Deployment tab and then the green plus icon under the “Deploy at the server startup” heading. Click Artifact, and then click the exploded war file artifact. Click OK. Change the “Application context” name for the artifact deployment to the server relative URL you want it deployed to.



6. Click Apply and then OK to save the run/debug configuration and dismiss the dialog.

Now that you have set up Tomcat in IntelliJ and configured an IntelliJ project to run in Tomcat, you’re ready to start the application and debug it within your IDE.

1. Open your project with IntelliJ IDEA.

2. Make sure that its run/debug configuration is properly configured to use your local Tomcat 8.0 application server.

3. When opened, click the Debug icon on the toolbar (highlighted by the mouse pointer in the figure below) or press Shift + F9 to compile and start your application in debug mode. IntelliJ should launch your default browser, and you should immediately hit the breakpoints in index.jsp.


You should again see the webpage to indicate that your application successfully deployed.

No comments:

Post a Comment