This post shows how to debug an application deployed in Apache Tomcat remotely (Tomcat is a popular Servlet container). This is valid for any other application server or any other Java application since the debug in remote is a feature of the JVM (Java Virtual Machine) over which any Java application is run.
For debugging a Java application remotely with Eclipse we need three things:
- Run the application indicating to the JVM that it has to be executed in debug mode.
- The source code of the application we want to debug.
- Configure Eclipse to debug the application.
Starting Apache Tomcat in debug mode
First of all we have to restart the Tomcat with debug option for this purpose we need to pass to JVM (Java Virtual Machine) the options
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
JVM opens a port in current machine allowing external applications (like Eclipse) to connect for debugging purposes.
To do that with Tomcat we have to set the environment variable JAVA_OPTS that is read by Tomcat in startup.sh file.
1) Set variable and make it available. we can choose the port number, in this example is used 8000 number.
export JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
2) Start Tomcat (from bin folder):
./startup.sh