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

Starting a Debugging Session with 'jdb'

This section provides a tutorial example on how to a debugging session with 'jdb' and run a Java application with debugging commands.

To test the debugger, I wrote the following simple Java application, Hello.java:

class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
   }
}

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

C:\herong>javac Hello.java

C:\herong>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:

C:\herong>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.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing J2SE 1.6.0 on Windows

 Installing J2SE 1.5.0 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

'jdb' - The Java Debugger

 'jdb' - Java Debugger Command and Options

Starting a Debugging Session with 'jdb'

 Debugging Applications with Separate 'jdb' Sessions

 Debugging Java Applications Remotely

 Listing Debugging Commands with 'help' Command

 PrimeNumberSeeker.java - Multi-Thread Sample Program

 Starting Debugging Session on a Multi-Thread Application

 Stepping through Statements of the Child Thread

 Checking Variable Values in a Debugging Session

 Debugging the Main Thread of a Multi-Thread Application

 Switching Execution Threads in a Debugging Session

 Suspending Main Thread to Debug Child Thread

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
Starting a Debugging Session with 'jdb'