A debugger stops a running program at a line you choose, so you can look at its variables and run it one step at a time. Neovim talks to debuggers through the Debug Adapter Protocol, the same protocol VS Code uses. The nvim-dap plugin is the client, ’s dap.core extra adds keys and a UI, and the Java extra plugs in java-debug-adapter, which loads to run and debug Java.
What the pieces do
- nvim-dap starts a debug session, sets breakpoints and sends “step” or “continue” to the adapter.
- nvim-dap-ui opens panels for variables, watches, the call stack and breakpoints.
- java-debug-adapter is installed by and runs inside jdtls. It starts or connects to the JVM and reports what the program is doing.
Launch the app under the debugger
Open CatalogController.java from module 9 of the companion code and press <leader>dc, “run or continue”. Because no session is running, a picker lists debug configurations. jdtls creates one for every class with a main method, so in the companion repository the list is long. Type ShopApp and choose “Launch module-09-catalog: info.mnafshin.springuniverse.shop.ShopApp”.
Spring Boot starts and the UI panels open around the code. The app listens on port 8080 as usual.
Stop only on the request you care about
Put the cursor on the return line of the catalog method:
@GetMapping("/catalog")
PagedModel<CatalogItem> catalog(Pageable pageable) {
return new PagedModel<>(repository.findCatalog(pageable));
}
<leader>db toggles a plain breakpoint, which stops on every request. Here we only want the second page, so press <leader>dB instead. It asks for a condition, a Java expression evaluated each time the line runs:
Breakpoint condition: pageable.getPageNumber() == 1
Now call the endpoint from a terminal. curl 'localhost:8080/catalog?page=0&size=2' answers at once with Coffee and Tea; the condition was false. curl 'localhost:8080/catalog?page=1&size=2' hangs, and Neovim jumps to the breakpoint line.
Read the call stack
The Stacks panel shows the thread, http-nio-8080-exec-4 in our run, and its frames. The top one is CatalogController.catalog(Pageable) at line 19, followed by reflection and Spring MVC frames. <leader>dk moves up one frame and <leader>dj back down. Generated frames that have no source file show a “Source missing” message instead of opening a file. The Scopes panel shows the variables of the selected frame, here pageable, a PageRequest, and this.
Evaluate and watch expressions
While paused you can run Java against the live objects. Select an expression and press <leader>de, or open the REPL with <leader>dr and type:
dap> pageable.getOffset()
2
Expressions can call methods on beans too: repository.findCatalog(pageable).getTotalElements() returned 4. Calling methods runs real code, so a query you evaluate really hits the database.
A watch is an expression the UI re-evaluates every time the program stops. In the Watches panel, enter insert mode, type pageable.getPageSize() * 10 and press Enter. It shows int = 20, and updates on the next stop.
Step and finish
<leader>dO steps over the current line, <leader>di steps into a call and <leader>do steps out. <leader>dc continues to the next breakpoint. Here, stepping over the return line left the controller and landed in Spring’s DirectMethodHandleAccessor, which is expected: the method was done. Continue, and the curl that was waiting prints Croissant and Espresso beans. <leader>dt ends the session and stops the app.
Attach to a JVM that is already running
On a server you rarely start the app from an editor. The JVM starts with the JDK’s debug agent, JDWP, listening on a port:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 \
-jar module-09-catalog/target/module-09-catalog-0.1.0-SNAPSHOT.jar
suspend=n lets the app start without waiting for a debugger, and the JVM prints “Listening for transport dt_socket at address: 5005”. With Maven, the same option goes into -Dspring-boot.run.jvmArguments.
The attach configuration needs a host, a port and the jdtls project. LazyVim’s built-in “Debug (Attach) - Remote” entry failed here with “Main class … isn’t unique in the workspace, please pass in specified projectname”, because several modules contain the same class name. A .vscode/launch.json entry with projectName works:
{ "type": "java", "request": "attach", "name": "Attach to a JVM on localhost:5005",
"hostName": "localhost", "port": 5005, "projectName": "module-09-catalog" }
Pick it with <leader>dc. It connected in under a second, and the conditional breakpoint, stack, scopes, watch, REPL and stepping above all worked. To leave without stopping the app, run :lua require("dap").disconnect({ terminateDebuggee = false }); the JVM kept serving requests and accepted the next attach.
JDWP has no password, so on a real server bind it to address=127.0.0.1:5005 and forward the port over SSH with ssh -L 5005:localhost:5005 user@server, then attach to localhost:5005. We tested this forward on one machine with a local SSH server, not against a remote host.
IntelliJ’s attach configuration does the same job; its extras, such as “Reset Frame” and logging breakpoints, have no equivalent here.
Recap
nvim-dap,nvim-dap-uiandjava-debug-adaptermake up the debugger;dap.coreand the Java extra set them up.<leader>dclaunches a main class such asShopApp, or attaches to a JVM started with-agentlib:jdwp(give the config aprojectName).<leader>dbsets a breakpoint,<leader>dBa conditional one such aspageable.getPageNumber() == 1.- The Stacks and Scopes panels show frames and variables;
<leader>dkand<leader>djmove between frames. <leader>deand the REPL evaluate Java while paused; the Watches panel keeps expressions up to date.
Series: Neovim as a Java IDE. Previous: IntelliJ IDEA features and their Neovim equivalents · Next: Running Java tests in Neovim
Sources
Official documentation and well-known tutorials read while writing this article. The wording here is our own.
- nvim-dap documentation
- nvim-dap-ui README
- LazyVim: dap.core extra
- nvim-jdtls: debugger (via nvim-dap)
- java-debug: launch and attach options
- Oracle: Java Debug Wire Protocol and the jdwp agent options
- IntelliJ IDEA: Debug code
- IntelliJ IDEA: Tutorial: Remote debug
- Spring Boot Maven plugin: running your application (
jvmArguments) - Baeldung: Remote Debugging with IntelliJ IDEA
- Lorenzo Bettini: Neovim and Java with LazyVim, part 4: Running programs
- Nirmal Ravidas: Setting Up a Full Spring Boot Dev Environment on Linux with Neovim and LazyVim
- Dhruva Sagar: Neovim as a Java IDE
Comments