Friday, 27 June 2014

Remote debuggin WEB applications with Intellij IDEA and Tomcat

Hello! Today I want to introduce you to the one great feature, a lot of young Java padawans don't know about!
Let's start from the traditional funny image:



All IDE users certainly know about a debugging console applications. But what about the debugging WEB-app? Wow, it is a real magic, we can simply press some button on your web-site and immediately jump to the first breakpoint, your program goes through. And we can march over our code within IDE in familiar way!
Oh! I was really inspired when have been told about this approach for the first time!

And today I've prepared some screenshots for you. Next we'll speak about remote debugging in Intellij IDEA with Tomcat.


In this tutorial I'll use my 'pet' project.

You must choose any of your web-apps to repeat all those steps by yourself.
If you haven't any projects, you can download from HERE my little example for previous post about Spring Java Config  and go through next steps using this project.

First, we need to configure Run/Debug configuration. It can be done by clicking 'Run' button in the main menu from above:


Then click '+' symbol to add new remote config:


Let's configure our command line arguments for running remote JVM. Type this settings in selected fields, define your configuration name and print it in input 'Name' field:


Press 'Apply' and 'OK' buttons.

Next we need to deploy our app to Tomcat server. I'll not explain how to do it in this post.

Then we must start our Tomcat server in specific way (Check before that server switched off).
Now let's start Tomcat using command prompt:

Command for Windows users: 
...[location of your Tomcat]/bin/catalina.bat jpda start

Command for Linux users: 
...[location of your Tomcat]/bin/catalina.sh jpda start

You will see something like this in command prompt after server started:



Next step is to run your app (In IDEA) in remote debug mode. To do this click 'Run' - Debug '[Defined name]':


The debug menu will appear bellow with the next text:


Good. Let's try to debug your app!
I have the web-app based on Spring MVC. When I'll go to the next URL (see img below) this request will be handled by controller class - 'HelloController'. It has one method with conforming 'RequestMapping' value.
According to this, I've put a breakpoint on the first line of the method. You must do the same things in your application.

Then open your browser and follow to your page, chosen for debug (I'll go to a front page of my project). Write your URL, press 'Enter' and you'll see how your browser will hang up and pass control to the Intellij.


The program will stop on the our first breakpoint:



That's all. Happy remote debugging!

Subscribe to my blog and share to your friends if you've liked it! Thanks!

No comments:

Post a Comment