Search code examples
javaneovimeclipse-jdt

Neovim setting up jdtls with lsp-zero/mason


As part of the upcoming 2023 new year I wanted to try and move my development environment to vim or neovim. I have gone through a bit of setup already and have go and js/ts setup and appearing to work just fine. Autocomplete, linting and import management.

Trying to get lsp-zero and java working though is turning out to be a nightmare (because of course java would be a problem child). I opened a java file lsp-zero was baller and asked to install the jdtls which appears to have worked and voila nothing... I just have code highlighting. No auto-complete or importing management.

Mason LSP Servers

I added the following to test

-- configure an individual server
lsp.configure('jdtls', {
  flags = {
    debounce_text_changes = 150,
  },
  on_attach = function(client, bufnr)
    print('lsp server (jdtls) attached')
  end
})

lsp.configure('gopls', {
  flags = {
    debounce_text_changes = 150,
  },
  on_attach = function(client, bufnr)
    print('lsp server (gopls) attached')
  end
})

Java is not picking up the lsp server enter image description here

Go picks up just fine enter image description here

Does anyone know of additional configs that are needed. I am not seeing anything specifically called out.

--- Config edit ---

I updated the config to call the windows version of the scripts. I also added a data path and root_dir. The lsp still never triggers.

require'lspconfig'.jdtls.setup{
 cmd = {
        'jdtls-win.cmd',
        "-configuration",
        "C:\\Users\\Coury\\AppData\\Local\\nvim-data\\mason\\packages\\jdtls\\config_win",
        "-jar",
        "C:\\Users\\Coury\\AppData\\Local\\nvim-data\\mason\\packages\\jdtls\\plugins\\org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar",
        "-data",
        "C:\\Users\\Coury\\Documents\\Code\\interviews\\truleo\\app",
    },
    single_file_support = true,
    root_dir = function() 
        return "C:\\Users\\Coury\\Documents\\Code\\interviews\\truleo\\app"
    end,
    flags = {
        debounce_text_changes = 150,
    },
    on_attach = function(client, bufnr)
        print('lsp server (jdtls) attached')
    end
}

Solution

  • Be sure to add your java path to your bashrc file and retry the installation via Mason

    If you're still encountering with issue setting up mason/jdlts, continue with the below instructions

    1. Install eclipse.jdt.ls by following their Installation instructions.

    2. Add the plugin: Plug mfussenegger/nvim-jdtls and packer.nvim: mfussenegger/nvim-jdtls

    3. Create your personal jdlts config file in your plugins directory with the below code.

    4. Source the new config and open any java file.

    
    -- Java.lua
    
    local config = {
        cmd = {
            --
            "java", -- Or the absolute path '/path/to/java11_or_newer/bin/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",
            "-Xms1g",
            "--add-modules=ALL-SYSTEM",
            "--add-opens",
            "java.base/java.util=ALL-UNNAMED",
            "--add-opens",
            "java.base/java.lang=ALL-UNNAMED",
            --
            "-jar",
            "/path/to/jdtls_install_location/plugins/org.eclipse.equinox.launcher_VERSION_NUMBER.jar",
            "-configuration", "/path/to/jdtls_install_location/config_SYSTEM",
            "-data", "/Users/YOUR_MACHINE_NAME/local/share/nvim/java"
        },
        settings = {
            java = {
                signatureHelp = {enabled = true},
                import = {enabled = true},
                rename = {enabled = true}
            }
        },
        init_options = {
            bundles = {}
        }
    }