JDB(1)
๐ฆ jdb โ the Java Debugger, aka JDB(1) โ find and fix bugs in Java platform programs ๐ฆ
๐ธ NAME ๐ธ
jdb is your command-line bestie for finding and fixing bugs in Java platform programs. periodt.
โจ SYNOPSIS โจ
jdb [options] [classname] [arguments]
- options โ This represents the
jdbcommand-line options. See Options for thejdbcommand. - classname โ This represents the name of the main class to debug.
- arguments โ This represents the arguments that are passed to the
main()method of the class.
๐ DESCRIPTION ๐
The Java Debugger, aka JDB, is a simple command-line debugger for Java classes. The jdb command and its options call the JDB. The jdb command demonstrates the Java Platform Debugger Architecture and provides inspection and debugging of a local or remote JVM. iconic behavior, honestly.
๐ START A JDB SESSION ๐
Okay so, there are many ways to start a JDB session. The most frequently used way is to have the JDB launch a new JVM with the main class of the application to be debugged. Do this by substituting the jdb command for the java command in the command line. For example, if your application's main class is MyClass, then use the following command to debug it under the JDB:
jdb MyClass
When started this way, the jdb command calls a second JVM with the specified parameters, loads the specified class, and stops the JVM before executing that class's first instruction. main character energy fr fr.
Another way to use the jdb command is by attaching it to a JVM that's already running. Syntax for starting a JVM to which the jdb command attaches when the JVM is running is as follows. This loads in-process debugging libraries and specifies the kind of connection to be made.
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n MyClass
You can then attach the jdb command to the JVM with the following command:
jdb -attach 8000
8000 is the address of the running JVM. iykyk.
The MyClass argument isn't specified in the jdb command line in this case because the jdb command is connecting to an existing JVM instead of launching a new JVM.
There are many other ways to connect the debugger to a JVM, and all of them are supported by the jdb command. The Java Platform Debugger Architecture has additional documentation on these connection options. slay, options on options.
๐ง BREAKPOINTS ๐ง
Breakpoints can be set in the JDB at line numbers or at the first instruction of a method, for example:
- The command
stop at MyClass:22sets a breakpoint at the first instruction for line 22 of the source file containingMyClass. - The command
stop in java.lang.String.lengthsets a breakpoint at the beginning of the methodjava.lang.String.length. - The command
stop in MyClass.<clinit>uses<clinit>to identify the static initialization code forMyClass.
When 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 the following syntax: clear MyClass:45. Using the clear or stop command with no argument displays a list of all breakpoints currently set. The cont command continues execution. literally the whole breakpoint loop, bestie.
๐ฆ STEPPING ๐ฆ
The step command advances execution to the next line whether it's in the current stack frame or a called method. The next command advances execution to the next line in the current stack frame. tiny steps, big debug energy.
๐ EXCEPTIONS ๐
When an exception occurs for which there isn't a catch statement anywhere in the throwing thread's call stack, the JVM typically prints an exception trace and exits. When running under the JDB, however, control returns to the JDB at the offending throw. You can then use the jdb command to diagnose the cause of the exception. dramatic but useful.
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 that's an instance of the specified class or subclass stops the application at the point where the exception is thrown.
The ignore command negates the effect of an earlier catch command. The ignore command doesn't cause the debugged JVM to ignore specific exceptions, but only to ignore the debugger. not the same thing, bestie โ read it twice.
๐ OPTIONS FOR THE JDB COMMAND ๐
When you use the jdb command instead of the java command on the command line, the jdb command accepts many of the same options as the java command. love a compatible icon.
The following options are accepted by the jdb command:
-helpโ Displays a help message.-sourcepath dir1:dir2:...โ Uses the specified path to search for source files in the specified path. If this option is not specified, then use the default path of dot (.).-attach addressโ Attaches the debugger to a running JVM with the default connection mechanism.-listen addressโ Waits for a running JVM to connect to the specified address with a standard connector.-listenanyโ Waits for a running JVM to connect at any available address using a standard connector.-launchโ Starts the debugged application immediately upon startup of thejdbcommand. The-launchoption removes the need for theruncommand. The debugged application is launched and then stopped just before the initial application class is loaded. At that point, you can set any necessary breakpoints and use thecontcommand to continue execution.-listconnectorsโ Lists the connectors available in this JVM.-connect connector-name:name1=value1....โ Connects to the target JVM with the named connector and listed argument values.-dbgtrace [flags]โ Prints information for debugging thejdbcommand.-tclientโ Runs the application in the Java HotSpot VM client.-trackallthreadsโ Tracks all threads as they are created, including virtual threads. See Working With Virtual Threads below.-tserverโ Runs the application in the Java HotSpot VM server.-Joptionโ Passes option to the JDB JVM, where option is one of the options described on the reference page for the Java application launcher. For example,-J-Xms48msets the startup memory to 48 MB. See Overview of Java Options injava.
The following options are forwarded to the debuggee process:
-Roptionโ Passes option to the debuggee JVM, where option is one of the options described on the reference page for the Java application launcher. For example,-R-Xms48msets the startup memory to 48 MB. See Overview of Java Options injava.-vor-verbose[:class|gc|jni]โ Turns on the verbose mode.-Dname=valueโ Sets a system property.-classpath dirโ Lists directories separated by colons in which to look for classes.-X optionโ A nonstandard target JVM option.
Other options are supported to provide alternate mechanisms for connecting the debugger to the JVM that it's to debug. more ways to connect, no cap.
๐ WORKING WITH VIRTUAL THREADS ๐
Often virtual threads are created in such large numbers and frequency that they can overwhelm a debugger. For this reason by default JDB does not keep track of virtual threads as they are created. It will only keep track of virtual threads that an event has arrived on, such as a breakpoint event. The -trackallthreads option can be used to make JDB track all virtual threads as they are created. vibe check: virtual threads can be a lot.
When JDB first connects, it requests a list of all known threads from the Debug Agent. By default the debug agent does not return any virtual threads in this list, once again because the list could be so large that it overwhelms the debugger. The Debug Agent has an includevirtualthreads option that can be enabled to change this behavior so all known virtual threads will be included in the list. The JDB -trackallthreads option will cause JDB to automatically enable the Debug Agent's includevirtualthreads option when JDB launches an application to debug. However, keep in mind that the Debug Agent may not know about any virtual threads that were created before JDB attached to the debugged application. important caveat, bestie โ don't forget it.
tiny reminder from the page: the Debug Agent may not know about virtual threads created before JDB attached. That's not a vibe, that's just a fact.
That's the whole manual, bestie โ brought to you by JDK 21, 2023, and the one and only JDB(1). Signed, sealed, debugged. ๐
โจ
๐ฆ where this page comes from
- centos-stream 10: java-21-openjdk-devel-slowdebug 1:21.0.12.1.1-2.1.el10 ยท ASL 1.1 and ASL 2.0 and BSD and BSD with advertising and GPL+ and GPLv2 and GPLv2 with exceptions and IJG and LGPLv2+ and MIT and MPLv2.0 and Public Domain and W3C and zlib and ISC and FTL and RSA
- Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms