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...)

27    public void run() {
28       primes = new int[ceiling];
29       while (count < ceiling) {
30          current++;
31          int j = 2;
32          boolean isPrime = true;
33          while (j<current/2 && isPrime) {
34             isPrime = current % j > 0;
35             j++;
36          }
37          if (isPrime) {
38             count++;
39             primes[count-1] = current;
40          }
41          try {
42             sleep(delay);
43          } catch (InterruptedException e) {
44             System.out.println("Runner interrupted.");
45          }
46       }
47    }
48 }

Note that:

  • This application tries to use a sub-thread to calculate prime numbers.
  • The main thread is monitoring how the sub-thread is doing.
  • A delay mechanism is used to slow the calculation.
  • The application has an infinite loop. So you have to terminate it by "Ctrl-C".

Here is how I compiled the application and executed without debugging:

>javac -g PrimeNumberSeeker.java

>java PrimeNumberSeeker
Period, Current int, # primes
1, 2, 0
2, 12, 5
3, 22, 8
4, 32, 11
5, 42, 13
6, 52, 15
7, 62, 18
8, 72, 20
9, 82, 22
10, 92, 24
11, 102, 26
12, 112, 29
13, 122, 30
...
53, 521, 98
54, 531, 99
55, 541, 100
56, 541, 100
57, 541, 100
...

The output seems to be fine. But I want to use "jdb" to exam the calculation and to practice the debugging commands. Below I will show my debugging session in multiple parts with my comments.

1. Setting up breakpoints and getting the debugging session going:

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

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

Breakpoint hit: "thread=main", PrimeNumberSeeker.main(), line=13 
13          System.out.println("Period, Current int, # primes");

(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