Debugging Java servlet with jdb. How to attach jdb with Tomcat

668 Views Asked by At

I have a tomcat running after making configuration and when I type jdb in terminal it says initializing. So I like to know

  1. How to attach jdb with tomcat and specify servlet name.

  2. What is the compile option (like in C its eg. -g with gdb) to include debugging symbols when compiling servlet with javac

I have opt/tomcat/apache-tomcat-10.0.10 so I guess Tomcat version is 10

1

There are 1 best solutions below

0
Piotr P. Karwasz On

The javac command switch to add all debugging symbols is ... -g. Line numbers and source file names are actually added by default, you only gain information on local variables (see this question).

You can not connect a debugger to a running JVM, unless it was started with the appropriate command line option (see this question).

Tomcat has a helper script bin/catalina.sh that can help you start it with the correct parameters:

  • catalina.sh jpda start starts Tomcat in the background with debugging enabled. You can connect to it with:

    jdb -attach localhost:8000
    
  • catalina.sh jpda run works as in the previous case, but in the foreground,

  • catalina.sh debug starts Tomcat through jdb. You just need to use run to start it.

Once you are connected you can use:

stop in <class id>.<method>

to add break points.

Remark: Both javac and jdb are not often used these days. Most people use tools like Ant, Maven, Gradle, etc. to compile their projects and IDEs to debug the code.