Skip to content

DAP support java

Zeioth edited this page Aug 12, 2023 · 7 revisions

Nothing special needs to be done for java.

How to configure DAP for java

Please note that this section has nothing to do with compiler.nvim. I'm documenting this to make your life easier. To debug java with DAP you have to:

  • Install java-debug-adapter and java-test using Mason.
  • Install the system dependency jdtls.
  • Then inside the root directory of your nvim config directory you must create /ftplugin/java.lua and paste this
-- HOW TO CONFIGURE THE JAVA DEBUGGER
-- -------------------------------------------------------------------------
-- https://github.com/mfussenegger/nvim-jdtls#configuration-quickstart
-- -------------------------------------------------------------------------
--
-- YOU MUST:
-- * Install the system dependency 'jdtls' in your operative system.
-- * Set the right path of your jdtls executable below in 'cmd'.
--
-- IF STILL FAILS CHECK:
-- * You have installed 'java-debug-adapter' on mason.
-- * The path of "bundles" if correct.

local config = {
    cmd = {'/usr/bin/jdtls'},
    root_dir = vim.fs.dirname(vim.fs.find({'gradlew', '.git', 'mvnw'}, { upward = true })[1]),
    init_options = {
      bundles = {
            vim.fn.glob(vim.fn.stdpath('data')..'/mason/packages/java-test/extension/server/*.jar', true ),
            vim.fn.glob(vim.fn.stdpath('data')..'/mason/packages/java-debug-adapter/extension/server/com.microsoft.java.debug.plugin-*.jar', true ),
        }
    }
}
require('jdtls').start_or_attach(config)

-- Give enough time for jdt to fully load the project, or it will fail with
-- "No LSP client found"
local timer = 2500
for i = 0, 12, 1 do
  vim.defer_fn(function() test = require('jdtls.dap').setup_dap_main_class_configs()
    end, timer)
  timer = timer + 2500
end

All there is left to do is adding a breakpoint to your code, and run :DapContinue screenshot_2023-08-09_02-11-22_086056873

Important for you to know

  • As you can see, the java DAP debugger is trickier to configure than the other debuggers, as it has its own way to do it. Hopefully the situation will change in the future.
  • If you find any issue while configuring DAP, run :DapSetLogLevel trace and :DapShowLog to find the reason.