Painless Java in Neovim
Just install and start writing public static void main(String[] args)
.
https://github.com/nvim-java/nvim-java/assets/18459807/047c8c46-9a0a-4869-b342-d5c2e15647bc
jdtls
jdtls
plugins
lombok
java-test
java-debug-adapter
Using lazy.nvim
return {
'nvim-java/nvim-java',
dependencies = {
'nvim-java/lua-async-await',
'nvim-java/nvim-java-core',
'nvim-java/nvim-java-test',
'nvim-java/nvim-java-dap',
'MunifTanjim/nui.nvim',
'neovim/nvim-lspconfig',
'mfussenegger/nvim-dap',
{
'williamboman/mason.nvim',
opts = {
registries = {
'github:nvim-java/mason-registry',
'github:mason-org/mason-registry',
},
},
}
},
}
lspconfig
require('java').setup()
require('lspconfig').jdtls.setup({})
Yep! That's all :)
JavaRunnerRunMain
- Runs the application or selected main class (if there
are multiple main classes):JavaRunnerRunMain
:JavaRunnerRunMain <arguments> <to> <pass>
JavaRunnerStopMain
- Stops the running applicationJavaRunnerToggleLogs
- Toggle between show & hide runner log windowJavaDapConfig
- DAP is autoconfigured on start up, but in case you want to
force configure it again, you can use this APIJavaTestRunCurrentClass
- Run the test class in the active bufferJavaTestDebugCurrentClass
- Debug the test class in the active bufferJavaTestRunCurrentMethod
- Run the test method on the cursorJavaTestDebugCurrentMethod
- Debug the test method on the cursorJavaTestViewLastReport
- Open the last test report in a popup windowJavaProfile
- Opens the profiles UIbuilt_in.run_app
- Runs the application or selected main class (if there
are multiple main classes)require('java').runner.built_in.run_app({})
require('java').runner.built_in.run_app({'arguments', 'to', 'pass', 'to', 'main'})
built_in.stop_app
- Stops the running applicationrequire('java').runner.built_in.stop_app()
built_in.toggle_logs
- Toggle between show & hide runner log windowrequire('java').runner.built_in.toggle_logs()
config_dap
- DAP is autoconfigured on start up, but in case you want to force
configure it again, you can use this APIrequire('java').dap.config_dap()
run_current_class
- Run the test class in the active bufferrequire('java').test.run_current_class()
debug_current_class
- Debug the test class in the active bufferrequire('java').test.debug_current_class()
run_current_method
- Run the test method on the cursorrequire('java').test.run_current_method()
debug_current_method
- Debug the test method on the cursorrequire('java').test.debug_current_method()
view_report
- Open the last test report in a popup windowrequire('java').test.view_last_report()
require('java').profile.ui()
Neoconf can be used to manage LSP
setting including jdtls. Neoconf allows global configuration as well as project
vice configurations. Here is how you can set Jdtls setting on neoconf.json
{
"lspconfig": {
"jdtls": {
"java.configuration.runtimes": [
{
"name": "JavaSE-21",
"path": "/opt/jdk-21",
"default": true
}
]
}
}
}
Pass the settings to Jdtls setup.
require('lspconfig').jdtls.setup({
settings = {
java = {
configuration = {
runtimes = {
{
name = "JavaSE-21",
path = "/opt/jdk-21",
default = true,
}
}
}
}
}
})
For most users changing the default configuration is not necessary. But if you want, following options are available
{
-- list of file that exists in root of the project
root_markers = {
'settings.gradle',
'settings.gradle.kts',
'pom.xml',
'build.gradle',
'mvnw',
'gradlew',
'build.gradle',
'build.gradle.kts',
'.git',
},
-- load java test plugins
java_test = {
enable = true,
},
-- load java debugger plugins
java_debug_adapter = {
enable = true,
},
jdk = {
-- install jdk using mason.nvim
auto_install = true,
},
notifications = {
-- enable 'Configuring DAP' & 'DAP configured' messages on start up
dap = true,
},
}
Following is the high level idea. Jdtls is the language server nvim-java communicates with. However, we don't have all the features we need just in Jdtls. So, we are loading java-test & java-debug-adapter extensions when we launch Jdtls. Once the language server is started, we communicate with the language server to do stuff.
For instance, to run the current test,
┌────────────┐ ┌────────────┐
│ │ │ │
│ Neovim │ │ VSCode │
│ │ │ │
└─────▲──────┘ └──────▲─────┘
│ │
│ │
│ │
│ │
┌───────▼───────┐ ┌──────────────▼──────────────┐
│ │ │ │
│ nvim-java │ │ Extension Pack for Java │
│ │ │ │
└───────▲───────┘ └──────────────▲──────────────┘
│ │
│ │
│ │
│ │
│ │
│ ┌───────────┐ │
│ │ │ │
└──────────────► JDTLS ◄────────────┘
│ │
└───▲───▲───┘
│ │
│ │
│ │
│ │
│ │
┌───────────────┐ │ │ ┌────────────────────────┐
│ │ │ │ │ │
│ java-test ◄────────┘ └─────────► java-debug-adapter │
│ │ │ │
└───────────────┘ └────────────────────────┘
nvim-jdtls is a plugin that follows "Keep it simple, stupid!" approach. If you love customizing things by yourself, then give nvim-jdtls a try. I may or may not have copied some code :wink: Beauty of Open source!