Thursday, June 19, 2008

Debugging remotely on tomcat using eclipse

Debugging was, is and always be one of the most potent tool in the hands of a developer. With most of the development exposed to me is in java. In spite of all my dislike to make it user friendly somehow in the end you have to make a web app out of it. Now if one is lucky then one might be writing code from scratch but most of the time its not the case. With companies following iterative policy of development some time or the other one will have to work on a monstrous code base. Now me being a fan of eclipse IDE will also like to debug on that. But one funny problem may happen in setting up the debugger in eclipse when your java web app is running remotely. In the sense you have your open in eclipse as a project but you run your web app directly from tomcat by making changes in tomcat config (server.xml) file. So debug under such a kind of scenario here are the steps


The steps below is for linux/unix m/c.

To debug the first step is to make sure that tomcat allows debugging:

1) Open your startup.sh

2) Add these two lines in the beginning which specifies debug on port
8000(This port could be different from the port you have exposed in
server.xml for your application)
export JPDA_ADDRESS=8000
export JPDA_TRANSPORT=dt_socket
3) Change the execute line at the end to include jpda start
exec "$PRGDIR"/"$EXECUTABLE" jpda start "$@"

Now start your tomcat. It will now allow debugging.

Allowing debugging is not enough. You should also be able to use the facility which you have allowed. Now there are various tools which could allow us to do it. I use eclipse so here are the steps to debug via eclipse.

1) Goto Run->Open Debug Dialog

2) Single-click the heading "Remote Java Application", then press the new
button (looks like a page with a plus on it).

3) On the dialog that appears, enter a meaningful name (like "<Your_ProjectName>(remote)" for this configuration).

4) From the same dialog, change to the "Source" tab, then click "Add".

5) On the dialog that appears, single-click "Java Project", then click
"OK".

6) On the dialog that appears, click "<Your_ProjectName>" (or check the projects
you wish to import), then click "OK".

7) Now click the "Connect" tab and un check the box marked "Allow termination of remote JVM". Do not check allow termination of remove VM. As each time you terminate the
debugging,the server will also be shutdown which you might not like.

8) Type in localhost (or the host you have mentioned in server.xml )in
Host Text Box

9) Type in 8000 (the port mentioned in tomcat startup.sh) in the Port
TextBox

10) Click "Apply" to save your changes.

11) Click "Debug" to start testing your configuration

12) Add breakpoints in your application

13) Open your browser and work with <Your_Project< until you reach your breakpoint. You should be directed into Eclipse where you will see the line of code with your breakpoint, a list of variables, etc., etc. And then you can gain knowledge about the flow and behavior of the code base

No comments: