scalameta / nvim-metals

A Metals plugin for Neovim
https://scalameta.org/metals/
Apache License 2.0
471 stars 75 forks source link

[JavaVersionError] in RPC to metals server #682

Closed CrackedPoly closed 4 months ago

CrackedPoly commented 4 months ago

I've enabled nvim-metals in LazyVim, but every time I open a scala project, I encounter this. image

In the log file, logs resides:

[START][2024-07-03 16:54:03] LSP logging initiated
[ERROR][2024-07-03 16:54:03] .../vim/lsp/rpc.lua:770    "rpc"   "/Users/jian/Library/Application Support/Coursier/bin/metals"   "stderr"    'Exception in thread "main" '
[ERROR][2024-07-03 16:54:03] .../vim/lsp/rpc.lua:770    "rpc"   "/Users/jian/Library/Application Support/Coursier/bin/metals"   "stderr"    "java.lang.UnsupportedClassVersionError: scala/meta/metals/Main has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0"
[ERROR][2024-07-03 16:54:03] .../vim/lsp/rpc.lua:770    "rpc"   "/Users/jian/Library/Application Support/Coursier/bin/metals"   "stderr"    "\n\tat java.lang.ClassLoader.defineClass1(Native Method)\n"
[ERROR][2024-07-03 16:54:03] .../vim/lsp/rpc.lua:770    "rpc"   "/Users/jian/Library/Application Support/Coursier/bin/metals"   "stderr"    "\tat java.lang.ClassLoader.defineClass(ClassLoader.java:756)\n\tat java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)\n\tat java.net.URLClassLoader.defineClass(URLClassLoader.java:468)\n\tat java.net.URLClassLoader.access$100(URLClassLoader.java:74)\n\tat java.net.URLClassLoader$1.run(URLClassLoader.java:369)\n\tat java.net.URLClassLoader$1.run(URLClassLoader.java:363)\n\tat java.security.AccessController.doPrivileged(Native Method)\n\tat java.net.URLClassLoader.findClass(URLClassLoader.java:362)\n\tat java.lang.ClassLoader.loadClass(ClassLoader.java:418)\n\tat java.lang.ClassLoader.loadClass(ClassLoader.java:351)\n\tat coursier.bootstrap.launcher.a.a(Unknown Source)\n\tat coursier.bootstrap.launcher.Launcher.main(Unknown Source)\n"
[WARN][2024-07-03 16:54:03] ...lsp/handlers.lua:135 "The language server copilot triggers a registerCapability handler for workspace/didChangeWorkspaceFolders despite dynamicRegistration set to false. Report upstream, this warning is harmless"

As a newbie to Java, I get the point that the metals executable is compiled by version 55 (Java 11), but locally I am using version 52 (Java 8). However, I can prove that my global Java version is 17 by typing "echo $JAVA_HOME".

So I am wondering which part of settings can correct the Java version and how?

PS: I've tried setting 'javaHome' manually, not working.

screen shot of MetalsInfo: image

CrackedPoly commented 4 months ago

Oh, I found a similar issue #380. My problem comes from the default Java version of the coursier, which is only available to coursier. When nvim-metals install metals by 'coursier bootstrap', the Java 8 is used.

So the solution is to change the default Java version of coursier, you can check coursier's docs.