Nvim-jdtls doens't work for a big java project

128 Views Asked by At

I'm working on a big Java project. The project uses Java 1.8 but I have installed Java 20 for jdtls. The projects uses only an and build and tomcat. I'm currently using eclipse for the project but I want to change to Neovim. I used vim for other languages but Java is my arch enemy. :)). The problem is that the jdtls doesn't seem to work, the "go to definition" function takes me to the import statement and nothing works. In eclipse I only have a few folders in build path and I tried to add them to the configuration also but it doesn't seem to work. I have a few errors in the console as well but they shouldn't interfere with the "go to definition" function. I'm using Neovim on windows 10. Here's my console:

[ERROR][2024-03-04 01:13:37] .../vim/lsp/rpc.lua:734    "rpc"   "C:\\Users\\myName\\jdk-20.0.2\\bin\\java.exe"  "stderr"    "WARNING: Using incubator modules: jdk.incubator.vector, jdk.incubator.concurrent\r\n"
[ERROR][2024-03-04 01:13:38] .../vim/lsp/rpc.lua:734    "rpc"   "C:\\Users\\myName\\jdk-20.0.2\\bin\\java.exe"  "stderr"    "Mar 04, 2024 1:13:38 AM org.apache.aries.spifly.BaseActivator log\r\nINFO: Registered provider ch.qos.logback.classic.servlet.LogbackServletContainerInitializer of service jakarta.servlet.ServletContainerInitializer in bundle ch.qos.logback.classic\r\n"
[ERROR][2024-03-04 01:13:38] .../vim/lsp/rpc.lua:734    "rpc"   "C:\\Users\\myName\\jdk-20.0.2\\bin\\java.exe"  "stderr"    "Mar 04, 2024 1:13:38 AM org.apache.aries.spifly.BaseActivator log\r\nINFO: Registered provider ch.qos.logback.classic.spi.LogbackServiceProvider of service org.slf4j.spi.SLF4JServiceProvider in bundle ch.qos.logback.classic\r\n"
[WARN][2024-03-04 01:13:40] ...lsp/handlers.lua:137 "The language server jdtls triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless"
[ERROR][2024-03-04 01:13:40] ...lsp/handlers.lua:535    "4 Mar 2024, 01:13:40 Command _java.reloadBundles.command not supported on client"

And here's my config:

return {
    { 'mfussenegger/nvim-dap' },
    { 
        'mfussenegger/nvim-jdtls',
        config = function() 

            local jdtls = require('jdtls')
            local root_markers = {'gui.bat', '.git', 'bin'}
            local root_dir = require('jdtls.setup').find_root(root_markers)
            local workspace_folder = "C:\\Users\\myName\\Workspace\\" .. vim.fn.fnamemodify(root_dir, ":p:h:t")
            function nnoremap(rhs, lhs, bufopts, desc)
                bufopts.desc = desc
                vim.keymap.set("n", rhs, lhs, bufopts)
            end

            local on_attach = function(client, bufnr)
                local bufopts = { noremap=true, silent=true, buffer=bufnr }
                nnoremap('gD', vim.lsp.buf.declaration, bufopts, "Go to declaration")
                nnoremap('gd', vim.lsp.buf.definition, bufopts, "Go to definition")
                nnoremap('gi', vim.lsp.buf.implementation, bufopts, "Go to implementation")
                nnoremap('K', vim.lsp.buf.hover, bufopts, "Hover text")
                nnoremap('<C-k>', vim.lsp.buf.signature_help, bufopts, "Show signature")
                nnoremap('<space>wa', vim.lsp.buf.add_workspace_folder, bufopts, "Add workspace folder")
                nnoremap('<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts, "Remove workspace folder")
                nnoremap('<space>wl', function()
                    print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
                    end, bufopts, "List workspace folders")
                nnoremap('<space>D', vim.lsp.buf.type_definition, bufopts, "Go to type definition")
                nnoremap('<space>rn', vim.lsp.buf.rename, bufopts, "Rename")
                nnoremap('<space>ca', vim.lsp.buf.code_action, bufopts, "Code actions")
                vim.keymap.set('v', "<space>ca", "<ESC><CMD>lua vim.lsp.buf.range_code_action()<CR>",
                    { noremap=true, silent=true, buffer=bufnr, desc = "Code actions" })
                nnoremap('<space>f', function() vim.lsp.buf.format { async = true } end, bufopts, "Format file")
                nnoremap("<C-o>", jdtls.organize_imports, bufopts, "Organize imports")
                nnoremap("<space>ev", jdtls.extract_variable, bufopts, "Extract variable")
                nnoremap("<space>ec", jdtls.extract_constant, bufopts, "Extract constant")
                vim.keymap.set('v', "<space>em", [[<ESC><CMD>lua require('jdtls').extract_method(true)<CR>]],
                    { noremap=true, silent=true, buffer=bufnr, desc = "Extract method" })
            end

            local config = {
                flags = {
                    debounce_text_changes = 80,
                },
                on_attach = on_attach,  
                root_dir = root_dir, 
                settings = {
                    java = {
                        format = {
                            settings = {
                                url = "C:\\Users\\myName\\Downloads\\eclipse-java-google-style.xml",
                                profile = "GoogleStyle",
                            },
                        },
                        signatureHelp = { enabled = true },
                        contentProvider = { preferred = 'fernflower' },
                        -- Specify any completion options
--                      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.*",
--                          },
--                      },
                        -- Specify any options for organizing imports
                        sources = {
                            organizeImports = {
                                starThreshold = 9999;
                                staticStarThreshold = 9999;
                            },
                        },
                        -- How code generation should act
                        codeGeneration = {
                            toString = {
                                template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}"
                            },
                            hashCodeEquals = {
                                useJava7Objects = true,
                            },
                            useBlocks = true,
                        },
                        configuration = {
                            runtimes = {
                                {
                                    name = "JavaSE-1.8",
                                    path = "C:\\jdk_1.8",
                                }
                            }
                        },
         -- I have tried do add the full paths and the relative paths but they don't seem to work at all.
                        build = {
                            sources = {
                                root_dir .. "path/in/my/project",
                                root_dir .. "another_path/in/my/project",
                                root_dir .. "more/path/in/my/project/src",
                                "path/in/my/project",
                                "another_path/in/my/project",
                                "more/path/in/my/project/src",
                            }
                        }
                    }
                },
                cmd = {
                    "C:\\Users\\myName\\jdk-20.0.2\\bin\\java.exe",
                    '-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',
                    '-jar', vim.fn.glob('C:\\Users\\myName\\Downloads\\jdtls\\plugins\\org.eclipse.equinox.launcher_*.jar'),
                                       '-configuration', 'C:\\Users\\myName\\Downloads\\jdtls\\config_win',
                                       '-data', workspace_folder,
                },
            }
                        jdtls.start_or_attach(config)
        end

    },
    { 'nvim-lua/plenary.nvim' }
}

I'm sorry but I don't really know how to give a little example. Any help is welcomed, thank you!

0

There are 0 best solutions below