Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix java.lang.UnsupportedClassVersionError: test (class file version 52.65535) was compiled with preview features that are unsupported?

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.

like image 963
Luty Avatar asked Apr 01 '19 03:04

Luty


2 Answers

In launch.json file, add "vmArgs": "--enable-preview" to your debug configuration.

{
   ...
   "vmArgs": "--enable-preview"
}
like image 71
jinbo wang Avatar answered Sep 17 '22 22:09

jinbo wang


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.

  1. 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

  2. After uninstallation, download and install the JDK 8 from oracle: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

  3. reopen your vscode project, do the following:

  4. open your launch.json file, add this line in configuration:

    "vmArgs": "--enable-preview"

  5. 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

  6. Press F5 your code will compile and run!

Reference: https://www.baeldung.com/java-lang-unsupportedclassversion

like image 21
Lychee Avatar answered Sep 17 '22 22:09

Lychee