- It was not setting the classpath and workspace dir properly on opening a java file due to which the
go to definitionfunctionality was not working well for standard classes. - using
nvim-jdtlsalong withautocmdallows to run the config everytime a java file with given root_dir is opened. autocmdacts as a even listener which performs certain task when a certain type of event is triggered, in this case opening of a java file.
Here are 5 key reasons why configuring the LSP for Java (jdtls) is more complex than for languages like C/C++ (clangd):
jdtlsrequires a unique workspace directory (-data) per project to store metadata like indexes and preferences.- Other LSPs like
clangddo not require this — they attach to the project root directly.
- Java heavily depends on classpath resolution to locate dependencies and standard libraries.
- Without
pom.xml,build.gradle, or.classpath, Java LSP cannot resolve symbols. - In contrast, C/C++ typically only needs a
compile_commands.jsonfile, which is optional for basic features.
jdtlsis based on the Eclipse language server, which requires:- A specific
launcher.jarfile - A platform-specific
config_<os>directory - Java-specific flags and environment setup
- A specific
- Other LSPs like
clangdare just single binary executables — simpler and self-contained.
jdtlsmust be launched usingrequire("jdtls").start_or_attach({ ... })at runtime per project.- It cannot be declared statically with
lspconfig.jdtls.setup()like other LSPs. - This adds conditional logic and often involves
autocmdorftplugin.
- Java LSP needs proper detection of the JDK to resolve even core classes like
StringorList. - Without JDK setup and classpath, you lose go-to-definition and autocomplete for standard Java classes.
- In C++, standard libraries are usually discovered automatically via the system compiler.