You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched the existing issues of plugins related to this issue
Neovim version (nvim -v)
0.10.0 release
Operating system/version
Arch Linux
Describe the bug
My default version of Java is 21, and I have set the default java path in lspconfig to the binary. When I attempt to run or debug, I receive an UnsupportedClassVersionError. I believe this is because openjdk-17 is hardcoded into the nvim-java plugin for builds, however it uses the specified runtime binary configured by lspconfig. I have resolved the issue by setting my default java runtime version to 17 and updating the required path, however it would be useful to be able to change the javac version as a config for the nvim-java in order to avoid ClassVersionErrors.
nvim-java should use the same javac version as the currently specified runtime version configured by jdtls. An option to set the sdk used by nvim-java would also fix this issue.
Tried this suggestion, jdtls builds using openjdk-21 when the project loads, however the runner and debugger in nvim-java keep trying to run the build using openjdk-17
@kyerpotts Sounds impossible. We retrieve the java executable information from jdtls meaning, if jdtls uses openjdk-21 for LS, it should be the same for run and debug as well. Has to be some cache issue. But try
jdk= {
-- install jdk using mason.nvimauto_install=true,
}
When setup. This will disable auto installation of jdk 17. Then remove the JDK from mason. Clear the .classpaths, .projects & jdtls cache. Make sure you have not set jdk-17 as the runtime in pom.xml. Try again.
On further inspection it looks like the problem is only for java classes that are not part of gradle or maven projects.
I was trying to debug a small program that had no need for a build tool. Is this expected behavior for nvim-java?
@kyerpotts Sounds impossible. We retrieve the java executable information from jdtls meaning, if jdtls uses openjdk-21 for LS, it should be the same for run and debug as well. Has to be some cache issue. But try
jdk= {
-- install jdk using mason.nvimauto_install=true,
}
When setup. This will disable auto installation of jdk 17. Then remove the JDK from mason. Clear the .classpaths, .projects & jdtls cache. Make sure you have not set jdk-17 as the runtime in pom.xml. Try again.
I think you meant this?
jdk= {
-- install jdk using mason.nvimauto_install=false,
}
Either way, no matter the setup, mason forces installation of openjdk-17 even after doing MasonUninstall...
and sure enough if you use jdk-21 it errors because of class version:
java.lang.UnsupportedClassVersionError: Preview features are not enabled for MyClass (class file
version 65.65535). Try running with '--enable-preview'
EDITED: You can kinda solved this by adding home above the config like so:
...--in jdtls setup of your lspconfigjava= {
home="PATH_TO_RUNTIME",
configuration= {
runtimes= {
{
name="JavaSE",
path="PATH_TO_RUNTIME",
default=true,
},
},
}
}
In particular, for the op @kyerpotts , since you're using archlinux. I recommend Arch's java-runtime-common package. This allows you to use the command archlinux-java set JDK_VERSION which I believe automatically symlink/set the runtime version to /usr/lib/jvm/default-runtime (which you can just put in the nvim-lspconfig) - allowing from switching version via cmdline instead of dealing with mason and whatnot.
EDITED2: It seems I have something messed up on my end other than this issue. I'll open a new issue and discuss there. okay it seems like it's something to do with jdtls not loading the packages used properly for the projects I'm using in jdk-21... said projects work in jdk-17... I'll debug this further but I think this is a niche thing so I won't put a new issue.
On further inspection it looks like the problem is only for java classes that are not part of gradle or maven projects. I was trying to debug a small program that had no need for a build tool. Is this expected behavior for nvim-java?
If the problem is not a part of a gradle or maven project, then it also doesn't know what to do on my end. With a proper project setup it works fine, however.
@Opposite34 The reason why mason install openjdk even when you have set it to false is because java.setup being called twice. First time with the configuration you have provided and the second time with the default empty table (by default auto_install is true). I have added validations to check if setup is being called twice. I had invalid config instructions for LazyVim so checkout the new config instructions to fix it.
@kyerpotts For runtime thing, I assumed it will use the default in the config. Once again, this could be because lspconfig.jdtls.setup() is being called more than once so your configurations are overridden by consequent calls. I have added validations for this as well, but validations might not warn you given the order of how the plugins are being loaded.
Either way, I'm adding a command to change the JDK. I think I might release it today assuming I can find the time.
@kyerpotts@Opposite34 I tested this. Plugin actually works as I expected but ~/.cache/nvim/jdtls has be removed upon runtime change because by default it's cached.
This issue you are facing is actually a limitation in JDTLS. As you can see from the readme, it compiles the source using 19 class version. But when we run, you are using the 21 version and it expect the class version to be 21.
To confirm the theory, I tried Java 19 to run and it works without any issue,
Temporary solution
You can use JavaProfile command to create a new profile to the main with --enable-preview vm flag.
I noticed that profile doesn't apply right away in the same session. That's a bug and obviously, I have to add --enable-preview for jdtls not supported versions. That will be done with this issue.
Activity
s1n7ax commentedon Jun 9, 2024
Try removing.classpath file and cache in ~/.cache/nvim/jdtld
kyerpotts commentedon Jun 10, 2024
Tried this suggestion, jdtls builds using openjdk-21 when the project loads, however the runner and debugger in nvim-java keep trying to run the build using openjdk-17
s1n7ax commentedon Jun 10, 2024
@kyerpotts Sounds impossible. We retrieve the java executable information from
jdtlsmeaning, if jdtls uses openjdk-21 for LS, it should be the same for run and debug as well. Has to be some cache issue. But tryWhen setup. This will disable auto installation of jdk 17. Then remove the JDK from
mason. Clear the.classpaths,.projects&jdtlscache. Make sure you have not set jdk-17 as the runtime in pom.xml. Try again.s1n7ax commentedon Jun 10, 2024
@kyerpotts
https://github.com/nvim-java/nvim-java/wiki/Q-&-A#no_entry-how-to-clean-rebuild
[-]bug: [/-][+]bug: jdtls is not using jdk-21[/+]s1n7ax commentedon Jun 10, 2024
@kyerpotts
Additionally, add this to make sure
kyerpotts commentedon Jun 11, 2024
On further inspection it looks like the problem is only for java classes that are not part of gradle or maven projects.
I was trying to debug a small program that had no need for a build tool. Is this expected behavior for nvim-java?
Opposite34 commentedon Jun 22, 2024
I think you meant this?
Either way, no matter the setup, mason forces installation of openjdk-17 even after doing MasonUninstall...
and sure enough if you use jdk-21 it errors because of class version:
EDITED: You can kinda solved this by adding
homeabove the config like so:In particular, for the op @kyerpotts , since you're using archlinux. I recommend Arch's java-runtime-common package. This allows you to use the command
archlinux-java set JDK_VERSIONwhich I believe automatically symlink/set the runtime version to/usr/lib/jvm/default-runtime(which you can just put in the nvim-lspconfig) - allowing from switching version via cmdline instead of dealing with mason and whatnot.EDITED2: It seems I have something messed up on my end other than this issue. I'll open a new issue and discuss there.okay it seems like it's something to do withjdtlsnot loading the packages used properly for the projects I'm using in jdk-21... said projects work in jdk-17... I'll debug this further but I think this is a niche thing so I won't put a new issue.If the problem is not a part of a gradle or maven project, then it also doesn't know what to do on my end. With a proper project setup it works fine, however.
s1n7ax commentedon Jul 7, 2024
@Opposite34 The reason why mason install
openjdkeven when you have set it tofalseis becausejava.setupbeing called twice. First time with the configuration you have provided and the second time with the default empty table (by defaultauto_installistrue). I have added validations to check if setup is being called twice. I had invalid config instructions forLazyVimso checkout the new config instructions to fix it.https://github.com/nvim-java/nvim-java?tab=readme-ov-file#hammer-how-to-install
@kyerpotts For runtime thing, I assumed it will use the default in the config. Once again, this could be because
lspconfig.jdtls.setup()is being called more than once so your configurations are overridden by consequent calls. I have added validations for this as well, but validations might not warn you given the order of how the plugins are being loaded.Either way, I'm adding a command to change the JDK. I think I might release it today assuming I can find the time.
s1n7ax commentedon Jul 7, 2024
Make sure to update
lazy.nvimto latest. I'm using a new feature inlazy.nvimto load deps in the latest install instructionss1n7ax commentedon Jul 8, 2024
Background Story
@kyerpotts @Opposite34 I tested this. Plugin actually works as I expected but
~/.cache/nvim/jdtlshas be removed upon runtime change because by default it's cached.This issue you are facing is actually a limitation in JDTLS. As you can see from the readme, it compiles the source using 19 class version. But when we run, you are using the 21 version and it expect the class version to be 21.
To confirm the theory, I tried Java 19 to run and it works without any issue,

Temporary solution
You can use
JavaProfilecommand to create a new profile to the main with--enable-previewvm flag.s1n7ax commentedon Jul 8, 2024
I noticed that profile doesn't apply right away in the same session. That's a bug and obviously, I have to add
--enable-previewforjdtlsnot supported versions. That will be done with this issue.#241