mfussenegger / nvim-jdtls

Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls
GNU General Public License v3.0
1.05k stars 61 forks source link

failed to attach to remote debuggee vm. reason java.net.connectexception connection refused #483

Closed EnriqueBravo115 closed 1 year ago

EnriqueBravo115 commented 1 year ago

LSP client configuration

Also i set: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

local home = os.getenv('HOME')
local jdtls = require('jdtls')

local root_markers = { 'gradlew', 'mvnw', '.git' }
local root_dir = require('jdtls.setup').find_root(root_markers)

local workspace_folder = home .. "/.cache/jdtls/workspace" .. vim.fn.fnamemodify(root_dir, ":p:h:t")

local bundles = {
    vim.fn.glob(
        home .. '/Applications/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar',
        "\n")
}

vim.list_extend(bundles,
    vim.split(vim.fn.glob(home .. "/Applications/vscode-java-test/server/*.jar"), '\n'))

local extendedClientCapabilities = jdtls.extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)

local config = {
    capabilities = capabilities,
    settings = {
        flags = {
            allow_incremental_sync = true,
        },
        init_options = {
        },
        java = {
            --            saveActions = {
            --                organizeImports = true,
            --            },
            format = {
                settings = {
                    url = home .. ".config/nvim/lang-servers/intellij-java-google-style.xml",
                    profile = "GoogleStyle",
                },
            },
            configuration = {
                updateBuildConfiguration = "interactive",
            },
            signatureHelp = { enabled = true },
            contentProvider = { preferred = 'fernflower' },
            implementationsCodeLens = {
                enabled = true,
            },
            referencesCodeLens = {
                enabled = true,
            },
            references = {
                includeDecompiledSources = true,
            },
            inlayHints = {
                parameterNames = {
                    enabled = "all", -- literals, all, none
                },
            },
            completion = {
                favoriteStaticMembers = {
                    "org.hamcrest.MatcherAssert.assertThat",
                    "org.hamcrest.Matchers.*",
                    "org.hamcrest.CoreMatchers.*",
                    "org.junit.jupiter.api.Assertions.*",
                    "java.util.Objects.requireNonNull",
                    "java.util.Objects.requireNonNullElse",
                    "org.mockito.Mockito.*"
                },
                filteredTypes = {
                    "com.sun.*",
                    "io.micrometer.shaded.*",
                    "java.awt.*",
                    "jdk.*", "sun.*",
                },
            },
            sources = {
                organizeImports = {
                    starThreshold = 9999,
                    staticStarThreshold = 9999,
                },
            },
            codeGeneration = {
                toString = {
                    template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}"
                },
                hashCodeEquals = {
                    useJava7Objects = true,
                },
                useBlocks = true,
            },
            extendedClientCapabilities = extendedClientCapabilities
        },
    },
    cmd = {
        "java",
        '-Declipse.application=org.eclipse.jdt.ls.core.id1',
        '-Dosgi.bundles.defaultStartLevel=4',
        '-Declipse.product=org.eclipse.jdt.ls.core.product',
        '-Dlog.protocol=true',
        '-Dlog.level=ALL',
        '-Xmx4g',
        '--add-modules=ALL-SYSTEM',
        '--add-opens', 'java.base/java.util=ALL-UNNAMED',
        '--add-opens', 'java.base/java.lang=ALL-UNNAMED',
        "-javaagent:" .. home .. "/Applications/lombok.jar",
        '-jar',
        vim.fn.glob(
            "/home/nullboy/Applications/jdtls/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar"),
        "-configuration", "/home/nullboy/Applications/jdtls/config_linux/",
        '-data', workspace_folder,
    },
    init_options = {
        bundles = bundles,
    },
}

config['on_attach'] = function(client, bufnr)
    local _, _ = pcall(vim.lsp.codelens.refresh)
    require('jdtls').setup_dap({ hotcodereplace = 'auto' })
    require("lsp-config").on_attach(client, bufnr)
    local status_ok, jdtls_dap = pcall(require, "jdtls.dap")
    if status_ok then
        jdtls_dap.setup_dap_main_class_configs()
    end
end

require('dap.ext.vscode').load_launchjs()

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
    pattern = { "*.java" },
    callback = function()
        local _, _ = pcall(vim.lsp.codelens.refresh)
    end,
})

jdtls.start_or_attach(config)

local set = vim.keymap.set

set('n', "<leader>df", function()
    if vim.bo.modified then
        vim.cmd('w')
    end
    jdtls.test_class()
end, opts)

set('n', "<leader>dn", function()
    if vim.bo.modified then
        vim.cmd('w')
    end
    jdtls.test_nearest_method({
        config_overrides = {
            stepFilters = {
                skipClasses = { "$JDK", "junit.*" },
                skipSynthetics = true
            }
        }
    })
end, opts)

Eclipse.jdt.ls version

1.22.0

Steps to Reproduce

When i set a breackpoint and choose the option attach in dap.configurations.java i get failed to attach to remote debuggee vm. reason java.net.connectexception connection refused

local dap = require('dap')
dap.configurations.java = {
    {
        name = "Debug (Attach) - Remote",
        type = "java",
        request = "attach",
        hostName = "127.0.0.1",
        port = 5005,
    },
    {
        name = "Debug (Launch)",
        type = "java",
        request = "launch",
        program = "",
    },
    {
        name = "Debug Non-Project class",
        type = "java",
        request = "launch",
        program = "${file}",
    },
}

Expected Result

Connect the debugger to applications springboot and get information in dap

Actual Result

[ERROR][2023-05-10 22:38:02] ...lsp/handlers.lua:535 "10 may. 2023 22:38:02 [error response][attach]: Failed to attach to remote debuggee VM. Reason: java.net.ConnectException: Conexión rehusada\nFailed to attach to remote debuggee VM.

mfussenegger commented 1 year ago

You need to start the java app with something like -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 so it's listening on 5005 for debugging.

EnriqueBravo115 commented 1 year ago

Thanks, I couldn't solve the problem but it's ready, i run springboot maven with:

mfussenegger commented 1 year ago

Do you still get the same error?

One issue with multi-module projects is that you'd need to add the projectName to the configuration.

E.g. a complete configuration may look like this:

https://github.com/crate/crate/blob/c23352e37ec3f17a030a07f40daa8669468e4a45/.vscode/launch.json#L38-L46

EnriqueBravo115 commented 1 year ago

No bro, i solve now, dont worry