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.

Installing Tomcat on Your Machine as a Command-Line Application

Most application developers need to run Tomcat only as a command-line application and usually only from their IDE. To do this, follow these steps:

1. Download the architecture-appropriate Windows zip (if you use Windows) or the non-Windows zip (if you use anything else) from the Tomcat 8.0 download page and unzip the directory.

2. Place the contents of the Tomcat directory in this zip file into the folder C:\Tomcat 8.0 on your local machine (or into the appropriate directory for a server in your operating system). For example, the webapps directory should now be located at C:\Tomcat 8.0\webapps.

3. If you use Windows 7 or newer, you need to change some permissions to make Tomcat accessible from your IDE. Right-click the Apache Software Foundation directory in C: and click Properties. On the Security tab, click the Edit button. Add your user or the Users group, and give that entry full control over the directory.

4. To configure Tomcat for its first use, start by opening the file conf/tomcat-users.xml in your favorite text editor. Place the following tag between the <tomcat-users> </tomcat-users> XML tags:
<user username="admin" password="admin" roles="manager-gui,admin-gui" />

5. Open the conf/web.xml file. Search the file for the text org.apache.jasper.servlet.JspServlet. Below the tag that contains this text are two <init-param> tags. Add the following init parameters below the existing init parameters:
<init-param>
     <param-name>compilerSourceVM</param-name>
     <param-value>1.8</param-value>
</init-param>
<init-param>
     <param-name>compilerTargetVM</param-name>
     <param-value>1.8</param-value>
</init-param>
By default, Tomcat 8.0 compiles JavaServer Pages files with Java SE 6 language support even if it runs on Java SE 8. These new Servlet init parameters instruct Tomcat to compile JSP files with Java SE 8 language features, instead.

6. After you make these changes and save these files, you should now be ready to start up Tomcat and make sure that it runs properly. Open up a command prompt and change your directory to the Tomcat home directory (C:\Tomcat 8.0).

7. Type the command echo %JAVA_HOME% (or echo $JAVA_HOME on a non-Windows operating system) and press Enter to check whether the JAVA_HOME environmental variable is properly set to your Java Development Kit (JDK) home directory. If it is not, configure the environmental variable, and then log out and back in before proceeding. Tomcat cannot run without this variable properly set.

8. Type the command bin\startup.bat (or bin/startup.sh if you do not use Windows) and press Enter. A Java console window should open showing the output of the running Tomcat process. After a few seconds, you should see the message “INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1827 ms” or something similar in the console window. This means Tomcat has started properly.

9. Open your favorite Web browser and navigate to http://localhost:8080/. You should see a page that looks like Figure below. This means that Tomcat is running and JSPs are compiling properly with Java SE 8. If this screen does not come up or you observe an error in the Java console, you need to check the preceding steps and possibly consult the Tomcat documentation.


When you finish using Tomcat, you can stop it by running the command bin\shutdown.bat (or bin/shutdown.sh) in the command prompt in the Tomcat 8.0 home directory. The Java console window should close, and Tomcat will stop.