Skip to content

DAP support java

Zeioth edited this page Aug 9, 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 python with DAP you have to:

  • Install java-debug-adapter 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.
--
-- IF STILL FAILS CHECK:
-- * Give enough time for LSP to load 100%, or you will see the alert:
--   "No configuration found for java"

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
local test = nil
for i = 0, 12, 1 do
  vim.defer_fn(function() test = require('jdtls.dap').setup_dap_main_class_configs()
    print(test)
    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.
Clone this wiki locally