Debugging with Tomcat and Eclipse using jpda

I’ve not had much need for debugging my servlets, but while I’m currently working on a legacy codebase, it’s a great means of stepping through what is really happening.
I’m not a fan of the “run Tomcat in Eclipse”, well, just because it feels ooky, and I’d rather run the two VM’s separately so I can bounce them independently, and because I write such crap code, I’d rather not crash both of them at the same time.
The information on how to make Tomcat and Eclipse play nicely together is a bit sparse, so I’ve provided a nice little summary here. This is working with Tomcat 5.0.X, Eclipse 3.2, JDK 1.4.2_*
Step 1. Configure Tomcat
(This is for the Win32 build with the spiffy UI for starting/stopping)
a) Open up the configuration GUI (“Configure Tomcat”)
b) Select the Java tab
c) Into the Java Options include (substituting the correct locations)

NB: These are all on separate lines, with a <CR> at each EOL
d) Select the Startup tab
e) Into the Arguments section include:

NB: These are all on separate lines, with a <CR> at each EOL
f) Start and Stop Tomcat completely
2. Configure Eclipse (well, not much really to configure)
a) While in the Java perspective select Run/Debug…
b) Choose “Remote Java Application” from the tree (right click/New)
c) The defaults are all that is required.
d) Click “Debug” in the bottom corner to start it now, or Close for later
3. Debugging the Application
a) Select the servlet/code that requires examination
b) Create a breakpoint in the code
c) Click on the “Debug” (if not already debugging) (*)
d) Click on the Debug Perspective (optional)
— this should show the Eclipse connected to Tomcat, and there should be a huge list Threads and the title of the Debug should be something like:
NameOfApplication [Remote Java Application]
Java HotSpot(TM) Server VM[localhost:8000]

e) Now just run the application as normal (via a browser or whatever)
f) Watch in amazement as Eclipse debugs the application at the breakpoint.
(*) If you get an error such as “Failed to connect to remote VM. Connection Refused”. This normally means that Tomcat isn’t started, _or_ there is already a debugging session started via jpda. Check Tomcat is running, and check the Debug perspective to make sure that it isn’t running.


13 thoughts on “Debugging with Tomcat and Eclipse using jpda

  1. Wow.
    Thanks for reminding me why I run Jetty 🙂
    (Note: Yes, I’m aware that most of that work is actually JPDA… but I’ve also never felt ooky about running Jetty inside my IDE)

  2. It’s more that I feel ooky about running any container inside my IDE.
    Unit testing is a different matter, and if I was doing funky unit testing with Jetty being started etc, then I wouldn’t mind so much.
    However, doing human based exporatory type testing, compiling JSP’s etc, just seems “wrong” when run out of an IDE.
    And yeah, Jetty does rock. I just happen to have a lot more knowledge about Tomcat.

  3. Feeling ooky is not a great way to make decisions.
    There really isn’t any real reasons for not running Jetty inside Eclipse or IntelliJ. It’s a separate VM so even if you do completely screw it up it’ll just die without affecting the IDE.
    Tomcat is an entirely different beast though, god knows whether you can actually run it straight away from the command line. Although I’ve seen Weblogic run inside IntelliJ so maybe it is actually possible…

  4. Very helpful. However I was still stumpped by continuing “Connection Refused” errors. Yes, Tomcat was running. No, there were no other debug sessions.
    Turns out if you start Tomcat using “Catalina.bat” then it doesn’t work. It’s not clear to me how the interaction between TomcatW.exe, Tomcat.exe, Catalina.bat is supposed to work … but if I did what you said (configure using Tomcat5W.exe) and then start Tomcat5.exe, then it worked.

  5. when i try to debug i got this error
    Failed to connect to remote VM. Connection Refused
    tomcat already running

  6. The bit about the CR after each option on the JVM is vital. I was getting Connection Refused until I added the CR between the last default option and the -Xdebug. Also, added a CR after the last option for good measure.

Comments are closed.