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.