I recieved this error when I trying to debug a java program with vscode:
java.lang.UnsupportedClassVersionError:
test (class file version 52.65535) was compiled with preview features that are unsupported.
This version of the Java Runtime only recognizes preview features for class file version 55.65535
Here is launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch) - Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Debug (Launch)-test",
"request": "launch",
"mainClass": "test"
}
]
}
Here is the version of java:
java --version:
openjdk 11.0.2 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.2)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.2, mixed mode, sharing)
javac --version:
javac 11.0.2
This program can run on ubuntu original terminal, but throws error in vscode.
it would be greatful if you can help me out.
In launch.json file, add "vmArgs": "--enable-preview" to your debug configuration.
{
...
"vmArgs": "--enable-preview"
}
Update: after intensive googling I think I found the answer:
This is due to a mismatch with the code version that's compiling in vscode and the JDK version you're running in your system.
In your VScode, your compiler is trying to compile using (class file version 52.65535), which is Java 8, meanwhile your system is running class file version 55.65535 which is Java 11. In this case, clean uninstall the Java 11 in your system first, follow the uninstall instructions here: https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html#A1096903
After uninstallation, download and install the JDK 8 from oracle: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
reopen your vscode project, do the following:
open your launch.json file, add this line in configuration:
"vmArgs": "--enable-preview"
inside your file your want to compile and run, press F1 in vscode and do the following:
Java: Clean the java language server workspace
Java: Force Java Compilation
Press F5 your code will compile and run!
Reference: https://www.baeldung.com/java-lang-unsupportedclassversion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With