Vscode-java-debug: java.lang.UnsupportedClassVersionError

Created on 31 Mar 2019  Â·  32Comments  Â·  Source: microsoft/vscode-java-debug

Unable to run debug against java files. Seems like vscode is compiling with one version and running with another.

Debug Console message:
java.lang.UnsupportedClassVersionError: HelloWorld has been compiled by a more recent version of the Java Runtime (class file version 52.65535), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"

Environment
  • Operating System:
    OS: Windows_NT x64 10.0.17763

  • JDK version:
    C:\Users\jstan>java -version
    openjdk version "1.8.0_202"
    OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_202-b08)
    OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.202-b08, mixed mode)

C:\Users\jstan>javac -version
javac 1.8.0_202

C:\Users\jstan>echo %JAVA_HOME%
C:\Program Files\AdoptOpenJDK\jdk-8.0.202.08\

Steps To Reproduce
  1. Install JDK
  2. Install VSCode
  3. Add Java Extension Pack
  4. Write simple HelloWorld java file
  5. Run Debug
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, world");
  }
}
Current Result

java.lang.UnsupportedClassVersionError: HelloWorld has been compiled by a more recent version of the Java Runtime (class file version 52.65535), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"

Expected Result

Hello, world

Additional Informations

Tried this on two different computers that did not have Java or VSCode installed. Tried completely removing VSCode and re-installing. Tried several different Java versions (RedHat, Oracle, Adopt.. ver 8 and 11). Followed steps under https://code.visualstudio.com/docs/java/java-tutorial

Most helpful comment

Resolved.
summary:
1.make sure uninstall jdk8 clean
2.install jdk11
3.add "vmArgs": "--enable-preview" in launch.json
4.F1, "Java: Clean ……" and "Java: Force ……"
5.run standalone file again

All 32 comments

I meet the same problem

could you guys invoke "Java: Clean the Java language server workspace" command or "Java: Force Java Compilation" to see whether fix this problem?

could you guys invoke "Java: Clean the Java language server workspace" command or "Java: Force Java Compilation" to see whether fix this problem?

It does not work for me.

Same issue here :(

Environment : Linux falcon 4.15.0-29deepin-generic #31 SMP Fri Jul 27 07:12:08 UTC 2018 x86_64 GNU/Linux

JAVA_HOME = /opt/jdk1.8.0_201

$: javac cd "/home/natesh/Desktop/CN group activity" ; /opt/jdk1.8.0_201/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=loc
alhost:34505 -Dfile.encoding=UTF-8 -cp /home/natesh/.config/Code/User/workspaceStorage/f25dfee3d49af82423e43be725d04ec1/redhat.java/jdt_ws/jdt.ls-jav
a-project/bin Main 
Picked up _JAVA_OPTIONS:   -Dawt.useSystemAAFontSettings=gasp
javac: invalid flag: /home/natesh/Desktop/CN group activity
Usage: javac <options> <source files>
use -help for a list of possible options
Picked up _JAVA_OPTIONS:   -Dawt.useSystemAAFontSettings=gasp
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: Main has been compiled by a more recent version of the Java Runtime (class file ve
rsion 52.65535), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

Neither command did anything for me

i am having the same problem. it was working in the am fine but after the last java update at 9:53 pm on 2019-03-29 i started having this problem. any suggestions would be appreciated. thanks

me too

same issue for me. Any one has a workaround ?

+1, I meet the same problem, I have tried to uninstall vscode and jdk1.8 (clear almost everything), and reinstall them, but it doesn't work. I believe my env is correct.

i meet same issue

Could you guys share your project here?

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

{
   ...
   "vmArgs": "--enable-preview"
}

@sparkrico YES, this issue only happens on standalone java files. No impact on maven, gradle, eclipse project. That's because redhat.[email protected] just released recently, it will auto enable enable-preview compiler option for standalone java files to support java 12. If your JDK is 8, this option doesn't work. For JDK 11 above, you could add "vmArgs": "--enable-preview" to your debug configuration as a workaround. However, we're dealing with this issue and will provide a fix.

**{
            "name": "Java Console App",
            "type": "java",
            "request": "launch",
            "stopOnEntry": true,
            "jdkPath": "${env:JAVA_HOME}/bin",
            "cwd": "${fileDirname}",
            "vmArgs": "--enable-preview" , 
            "console": "integratedTerminal",
            "startupClass": "${fileBasenameNoExtension}",
            "classpath": [
                ".",
                "${fileDirname}"
            ],
            "externalConsole": true
        },**

It shows me unrecognized option '--enable-preview' .

Unrecognized option: --enable-preview
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

I was using jdk8 and now removed it installed jdk 11 . But the following error occurs when using the 'vmArgs' option .

usr/lib/jvm/java-11-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:45357 --enable-preview -Dfile.encoding=UTF-8 -cp "/home/natesh/.config/Code/User/workspaceStorage/f25dfee3d49af82423e43be725d04ec1/redhat.java/jdt_ws/CN group activity_fc7b231d/bin" Main 
Picked up _JAVA_OPTIONS:   -Dawt.useSystemAAFontSettings=gasp
Error: LinkageError occurred while loading main class Main
        java.lang.UnsupportedClassVersionError: Main (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

@nateshmbhat This option doesn't work for java 8. We're trying to provide a fix on redhat.java side.

ya even after installing jdk11 , it shows the above error. What's the workaround ?

Invoke "Java: Clean the Java language server workspace" command or "Java: Force Java Compilation" to see whether fix this problem

Invoke "Java: Clean the Java language server workspace" command or "Java: Force Java Compilation" to see whether fix this problem

It did . Thank you :)

Resolved.
summary:
1.make sure uninstall jdk8 clean
2.install jdk11
3.add "vmArgs": "--enable-preview" in launch.json
4.F1, "Java: Clean ……" and "Java: Force ……"
5.run standalone file again

Resolved following @medsonk's instructions

@sparkrico YES, this issue only happens on standalone java files. No impact on maven, gradle, eclipse project. That's because redhat.[email protected] just released recently, it will auto enable enable-preview compiler option for standalone java files to support java 12. If your JDK is 8, this option doesn't work. For JDK 11 above, you could add "vmArgs": "--enable-preview" to your debug configuration as a workaround. However, we're dealing with this issue and will provide a fix.

@testforstephen yes , i was using jdk8.
I finally understood why.
Thank you.

When is this problem fixed "officially"?

The hotfix release redhat.[email protected] is published, please update the extension. Meanwhile, you might need to clean the workspace folders though, after reopening your folder.

faced the same issue, can be solved by following the below steps:

  • uninstall jdk, jre
  • uninstall vs code
  • install vs code with java packet installer from the below link
    https://code.visualstudio.com/docs/languages/java
  • after installation, add extension - Java extension pack by microsoft
  • set path in preferences for the jdk installed by vscode bundle (which will be automatically set in most cases)
  • close and re open.... should work
    Worked for me

i have same issue how to solve this problem please suggest me

I encountered exactly the same issue in my Mac + VS Code.
The root cause was that VS Code was using JDK 1.8 while I was trying to use JDK 11 specified in my Maven.

The way how I fixed it was like this:

  1. Code -> Preferences -> Settings
  2. Search by keyword: jdk
  3. There is a "Java: Home" session, click the "Edit in settings.json"
  4. Identify the Java home path in Mac for JDK 11, by:
$ /usr/libexec/java_home -v11
/Library/Java/JavaVirtualMachines/adoptopenjdk-11-openj9.jdk/Contents/Home

Note: I'm using OpenJDK with J9 here, as you may have seen

  1. Change the "java.home" line to make sure it points to the right path:
"java.home": "/Library/Java/JavaVirtualMachines/adoptopenjdk-11-openj9.jdk/Contents/Home",

VS Code would prompt for restart, follow it and restart and everything just works fine.

this problem is still there even in the latest version. totally depressed right now.

@tripathi-abhishek Please open a new issue and provide reproduce steps.

@testforstephen @tripathi-abhishek I have faced this issue as well and I simply updated my system's (Windows) environment path for JAVA_HOME with JDK11 and it worked.

after 2 years we still don't have proper solution for this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Urastor picture Urastor  Â·  4Comments

radu-ion picture radu-ion  Â·  5Comments

bsaby picture bsaby  Â·  8Comments

mojo2012 picture mojo2012  Â·  5Comments

llgcode picture llgcode  Â·  7Comments