Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Discussing 5+ Ways to Trace Java Execution

Discussing 5+ Ways to Trace Java Execution

This item in japanese

Zviki Cohen has published Five ways for tracing Java execution, based on his experience exploring and having to understand code he didn't write. He's found that simply reading source code (and in some cases decompiling), can be a very tedious and error prone process. Instead, he recommends five different runtime tracing methods to observe Java code while it is executing, eliminating many of the disadvantages of trying to learn someone else's code. Here is a condensed list of his five suggestions:


  1. The Basic: Breakpoints and Step-by-Step Execution "Start with the simplest way: set up breakpoints and start tracing your execution. It is best when: You need a quick and simple solution, you have all the code and you know where you want to stop. You need elaborate information in a given point (arguments, local variables, etc.)."
  2. The Primal: Debug Messages "We continue by setting up debug messages. The simplest way is to use System.out.println statements to print out messages to the console. It is best when: You own the code and you have a good idea of what you're looking for. Very good solution for event handlers. It's high performance makes it practical for understanding which event is fired when throughout the execution of complex flows."
  3. The Hot Shot: Dynamic Proxy "An improvement over simple debug messages. Dynamic Proxy is a special Java feature which enables the developer to introduce a proxy class, sitting in front of a given class and intercepting all the calls through a given interface. It is best when: It's a great solution for event handlers. You can set up a dummy event handler with a generic proxy in seconds and see the sequence of events. This is simplest and quickest method when it comes to understanding event handlers."
  4. The Brute Force: Run-time Profiler "Profilers are powerful tools that trace all the calls in the system through special JVM hooks. However, it's like using a 10 pound hammer on a half inch nail. It is best when: You want a complete picture for a very specific operation (i.e. very short execution flow)."
  5. The New Age: Aspects "Aspect Oriented Programming (AOP) is a non-trivial idea. Without going into the concept of Aspects, I'm just looking at the bottom line: it's a quick and easy way of intercepting the execution of your code. You can selectively set hooks around methods, constructors, field access, etc., without modifying the original code. In these hooks you can print debug messages. It is best when: You want to trace the execution of a code you can rebuild."

Until February 2007, Zviki Cohen was an architect and senior consultant for Amdocs, and is now a private entrepreneur in the software space.

Rate this Article