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.