
jdb Command in Linux
jdb is a simple command-line debugger for Java programs. It is a part of the Java Development Kit (JDK) that helps developers find and fix bugs in their Java code.
With jdb, you can set breakpoints, inspect variables and steps through code to understand the working behind it. You can also use it to debug both local and remote Java applications. This command is pretty useful in case you want to debug programs running on a Virtual Machine (JVM).
Table of Contents
Here is a comprehensive guide to the options available with the jdb command −
Syntax for jdb Command
The basic syntax to use the jdb command in Linux is as follows −
jdb [options] [class] [arguments]
Here,
- options are various options to control the debugging session.
- class is the name of the Java class you want to debug.
- arguments are any arguments that your Java program requires.
jdb Command Options
The following are a few additional options that can be used with jdb to modify the debugging process −
Option | Description |
---|---|
-attach | Connects jdb to a running Java Virtual Machine (JVM) using a specific address or process ID. |
-sourcepath | Specifies the directories where the source files are located. |
-classpath | Sets the classpath for finding user classes and packages. |
-connect | Connects to a remote JVM for debugging. |
-dbgtrace | Enables tracing of the debugger's activities. |
-help | Displays a help message with a list of available commands and options. |
-listen | Displays a help message with a list of available commands and options. |
-listenany | Similar to -listen, but listens on any available network interface. |
-launch | Launches a new JVM and attaches jdb to it. |
-listenconnectors | Lists all available connectors that can be used to attach to a JVM. |
-tclient | Uses the client JVM, which is optimized for quick startup and smaller memory usage. |
-tserver | Uses the server JVM, which is optimized for maximum performance. |
-trackallthreads | Tracks all threads in the JVM, not just the main thread. |
Examples of jdb Command in Linux
Let's discuss a few examples of jdb command in Linux system −
- Basic Debugging Session
- Attach to an Existing JVM
- Set BreakPoints
- Run the Program
- Inspect Variables
- Step Through Code
- List Threads
Basic Debugging Session
To start a debugging session for a class named MyClass, you can initiate a new Java Virtual Machine (JVM) with the following command −
jdb MyClass
This command will launch MyClass in debug mode and allow you to interactively debug the class by setting breakpoints, inspecting variables, and stepping through the code.
Attach to an Existing JVM
If you have a JVM already running with debugging enabled, you can attach jdb to it. First, start your JVM with debugging options −
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n MyClass
This command starts MyClass with a debugger listening on port 8000.
Then, attach jdb to the running JVM using the below-given command −
jdb -attach 8000
This allows you to connect to the JVM and start debugging. Thus, providing the ability to control the execution and inspect the state of the running application.
Set Breakpoints
In case you want to set a breakpoint at a specific line in a class, simply use −
stop in MyClass:25
This sets a breakpoint at line 25 in MyClass, pausing execution when this line is reached.
Alternatively, to set a breakpoint at the beginning of a method −
stop at MyClass.myMethod
This sets a breakpoint at the start of the myMethod method in MyClass, allowing you to inspect the state before the method executes.
Run the Program
To start or continue the execution of the program, you can use −
run
Or
cont
run starts the program from the beginning, while cont continues execution after hitting a breakpoint. Thus, enables you to proceed with debugging after inspecting the current state.
Inspect Variables
To print the value of a variable, use −
print myVariable
This command displays the current value of myVariable, helping you understand its state at a specific point in execution. To dump all fields of an object −
dump myObject
This command shows all the fields and their values for myObject, providing a comprehensive view of the object’s state.
Step Through Code
To step through the code line by line, use −
step
This command moves the debugger to the next line of code, allowing you to follow the program's execution closely and understand how each line affects the program's state.
List Threads
To list all threads currently running in the JVM, use −
threads
This command displays a list of all active threads, which can be useful for debugging multi-threaded applications by showing you the state and activity of each thread.
That's how you can use the jdb command on a Linux system.
Conclusion
The jdb is a versatile command-line debugger for Java programs, included in the Java Development Kit (JDK). This guide has explored the syntax, various options, and practical examples of using the jdb command in Linux.
By mastering the jdb command, you can effectively debug Java applications, whether they are running locally or remotely. Becoming proficient with jdb can significantly enhance your productivity in identifying and resolving issues in your Java code, thus, making it an invaluable tool for Java developers.