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
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):
Configuring Eclipse to remote debugging
First of all, we have to open or import the project with the source code of the application we want to debug. Afterward we have to setup the debug from Eclipse:
1) Clicking on the right side (arrow facing to above) of the “Debug” button (the button with the bug) that is in the toolbar and the clicking on “Debug configurations…” or in the menu Run > Debug configurations… Then a new window with the different debug configuration will be opened.
2) Select “Remote Java Application” in the left menu and then press on “New launch configuration” button (the button with the folder icon in the top-left corner).
3) Fill the configuration with the data corresponding with our Tomcat instance:
- Project: The project we want to debug: it has to contain the source code for the application we want to debug.
- Connection Type: Standard (Socket Attach).
- Host: IP of the server in which is Tomcat installed.
- Port: Port indicated to JVM in address parameter. Port 8000 in our example
Remote Debugging of Tomcat with Eclipse
4) Execute the debug. We have to click on the button “Debug” and Eclipse will connect with Tomcat (it had to be previously running as I mentioned in this post). Now we have to put the breakpoints inside the Eclipse project to debug as we do with a regular project running in a local Tomcat.
And that’s all, if everything works well you can debug your Tomcat remotely. Now there is a long work to do debugging your app. Good luck!