jdb helps you find and fix bugs in Java language programs.
jdb [ options ] [ class ] [ arguments ]
options
class
arguments
main()
method of
class
.The Java Debugger, jdb, is a simple command-line debugger for Java classes. It is a demonstration of the Java Platform Debugger Architecture that provides inspection and debugging of a local or remote Java Virtual Machine.
There are many ways to start a jdb session. The most frequently used way is to have jdb launch a new Java Virtual Machine (VM) with the main class of the application to be debugged. This is done by substituting the command jdb for java in the command line. For example, if your application's main class is MyClass, you use the following command to debug it under JDB:
% jdb MyClass
When started this way, jdb invokes a second Java VM with any specified parameters, loads the specified class, and stops the VM before executing that class's first instruction.
Another way to use jdb is by attaching it to a Java VM that is already running. Syntax for Starting a VM to which jdb will attach when the VM is running is as follows. This loads in-process debugging libraries and specifies the kind of connection to be made.
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n
For example, the following command will run the MyClass application, and allow jdb to connect to it at a later time.
% java -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n MyClass
You can then attach jdb to the VM with the following commmand:
% jdb -attach 8000
Note that "MyClass" is not specified in the jdb command line in this case because jdb is connecting to an existing VM instead of launching a new one.
There are many other ways to connect the debugger to a VM, and all of them are supported by jdb. The Java Platform Debugger Architecture has additional documentation on these connection options. For information on starting a J2SE 1.4.2 or early VM for use with jdb see the 1.4.2 documentation
The following is a list of the basic jdb commands. The Java debugger supports other commands which you can list using jdb's help command.
4. (java.lang.Thread)0x1 main runningIn this example, the thread index is 4, the thread is an instance of java.lang.Thread, the thread name is "main", and it is currently running,
where
with no arguments dumps the stack of the
current thread. where all
dumps the stack of all
threads in the current thread group. where
threadindex dumps the stack of the specified thread.
Breakpoints can be set in jdb at line numbers or at the first instruction of a method, for example:
If a method is overloaded, you must also specify its argument types so that the proper method can be selected for a breakpoint. For example, "MyClass.myMethod(int,java.lang.String)", or "MyClass.myMethod()".
The clear command removes breakpoints using a syntax as in "clear MyClass:45". Using the clear or command with no argument displays a list of all breakpoints currently set. The cont command continues execution.
The step commands advances execution to the next line whether it is in the current stack frame or a called method. The next command advances execution to the next line in the current stack frame.
When an exception occurs for which there isn't a catch statement anywhere in the throwing thread's call stack, the VM normally prints an exception trace and exits. When running under jdb, however, control returns to jdb at the offending throw. You can then use jdb to diagnose the cause of the exception.
Use the catch command to cause the debugged application to stop at other thrown exceptions, for example: "catch java.io.FileNotFoundException" or "catch mypackage.BigTroubleException. Any exception which is an instance of the specifield class (or of a subclass) will stop the application at the point where it is thrown.
The ignore command negates the effect of a previous catch command.
NOTE: The ignore command does not cause the debugged VM to ignore specific exceptions, only the debugger.
When you use jdb in place of the Java application launcher on the command line, jdb accepts many of the same options as the java command, including -D, -classpath, and -X<option>.
The following additional options are accepted by jdb:
Other options are supported for alternate mechanisms for connecting the debugger and the VM it is to debug. The Java Platform Debugger Architecture has additional documentation on these connection alternatives.