Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.12, 2006

'jdb' - The Java Debugger

Part:   1  2   3  4  5  6  7  8  9 

Java Tool Tutorials

© 2006 Dr. Herong Yang

Latest updates:

  'javac' - The Java Compiler

  'java' - The Java Launcher

  'jdb' - The Java Debugger

  JAR File & 'jar' Tool

  Certificates and 'keytool'

  Installing J2SE 1.5.0

... Table of Contents

(Continued from previous part...)

Here is what I did in a command window to run "jdb" to launch and debug Hello.java:

>javac Hello.java

>jdb Hello
Initializing jdb ...

> stop in Hello.main
Deferring breakpoint Hello.main.
It will be set after the class is loaded.

> run
run Hello
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint Hello.main

Breakpoint hit: "thread=main", Hello.main(), line=3 bci=0
3          System.out.println("Hello world!");

main[1] next
Hello world!
>
Step completed: "thread=main", Hello.main(), line=4 bci=8
4       }

main[1] cont
>
The application exited

Notice that:

  • Once started, "jdb" offers you command prompt to allow you to run debugging commands interactively.
  • Without the "-launch" option, "jdb" will not start the main() method of the specified class.
  • "stop in" command sets a breakpoint at the beginning of the specified method. See the next section for other commonly used debugging commands.
  • "run" command starts a new JVM process with run your application in debug mode.
  • "jdb" and the JVM of your application are different processes. If you use Windows Task Manager, you should see two processes named as "jdb" and "java".
  • "next" command executes only the current statement of the debugged application.
  • "cont" command resumes the execution to the end or the next breakpoint.

If you want to launch the target application immediately, you can use the "-launch" option:

>jdb -launch Hello
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...
>
VM Started: No frames on the current call stack

main[1] cont
> Hello world!

The application has been disconnected

As we expected, "jdb -launch" command launches the target application immediately, and stops at the beginning of the main() method.

Attaching "jdb" to Running Applications

If you ask "Can I launch the debugger ('jdb') and the target application separately?", the answer is "Yes, you can.". In JDK 1.5, both "jdb" and "java" have been enhanced to use the latest JPDA (Java Platform Debugger Architecture) technology to give you the following options:

                     Debugger                 Target
Option                    jdb                   java

     1   Shared memory client   Shared memory server
     2   Shared memory server   Shared memory client
     3          Socket client          Socket server   
     4          Socket server          Socket client   

The shared memory options requires that the debugger and the target application to be on the same machine. Of course, the socket options allow you to run them remotely.

Let's try option #1 first. Open a command window on a windows system and run:

>java -agentlib:jdwp=transport=dt_shmem,address=MyHello,server=y,
   suspend=y Hello

Listening for transport dt_shmem at address: MyHello

(Continued on next part...)

Part:   1  2   3  4  5  6  7  8  9 

Dr. Herong Yang, updated in 2006
Java Tool Tutorials - Herong's Tutorial Notes - 'jdb' - The Java Debugger