54

I get this problem Using IntelliJ. But I have the newest version of everything newly installed on my system.

... has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0

I've set:

PATH as C:\Program Files\Java\jdk-13 JAVA_HOME as: C:\Program Files\Java\jdk-13 JRE_HOME as: C:\Program Files\Java\jre1.8.0_221

I've set the path, tried to find a change in the Project structure

COMPLETE ERROR MESSAGE:

H:\087-JAVA\HelloWorld\src>java com.codewithmosh.Main
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/codewithmosh/Main has been compiled by a more recent version of the Java Runtime (class file version 57.0), 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(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
CrazyCoder
  • 350,772
  • 137
  • 894
  • 800
nobism
  • 557
  • 1
  • 3
  • 3
  • 1
    What's `java -version` output? – CrazyCoder Sep 26 '19 at 23:25
  • What happens when you run `C:\Program Files\Java\jdk-13\bin\java com.codewithmosh.Main`? – Thorbjørn Ravn Andersen Sep 26 '19 at 23:35
  • See the answer below. If removing the [highlighted path](https://i.imgur.com/bXhEiFC.png) followed by the reboot helps, feel free to [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – CrazyCoder Sep 27 '19 at 23:26
  • Had same issue with Eclipse. I have openJDK8 (aka java 1.8; also includes JRE) installed. In eclipse I had selected JRE OpenJDK8 to be used but did not realize I had to set the compiler separately. In preferences > Java > Compiler > Compiler compliance level -> 1.8 – Touten Nov 12 '20 at 22:40

9 Answers9

32

You need to double check the PATH environment setting. C:\Program Files\Java\jdk-13 you currently have there is not correct. Please make sure you have the bin subdirectory for the latest JDK version at the top of the PATH list.

java.exe executable is in C:\Program Files\Java\jdk-13\bin directory, so that is what you need to have in PATH.

Use this tool to quickly verify or edit the environment variables on Windows. It allows to reorder PATH entries. It will also highlight invalid paths in red.

If you want your code to run on lower JDK versions as well, change the target bytecode version in the IDE. See this answer for the relevant screenshots.

See also this answer for the Java class file versions. What happens is that you build the code with Java 13 and 13 language level bytecode (target) and try to run it with Java 8 which is the first (default) Java version according to the PATH variable configuration.

The solution is to have Java 13 bin directory in PATH above or instead of Java 8. On Windows you may have C:\Program Files (x86)\Common Files\Oracle\Java\javapath added to PATH automatically which points to Java 8 now:

javapath

If it's the case, remove the highlighted part from PATH and then logout/login or reboot for the changes to have effect. You need to Restart as administrator first to be able to edit the System variables (see the button on the top right of the system variables column).

CrazyCoder
  • 350,772
  • 137
  • 894
  • 800
  • 2
    This is a great answer. Moving the "Path" of the latest java version up and above any other version in the System Environment variables solves the problem. Be sure to sign in and out, or restart, afterwards – Chidozie Nnachor Dec 17 '19 at 15:15
  • 3
    Don't forget to close and reopen the command line after setting the environment variables. It seems to me a command line window remembers the prev state of the environment variables. (this of course only applies if you're trying to run it from the command line) – shieldgenerator7 Feb 03 '20 at 23:17
10

I also encountered similar problem which is asked here. The issue was that some applications come with their own JRE and sometimes the installed JDK appears at lower priority level in environment path. Now there are two options:

  1. Uninstall the other application which has their own JDK/JRE.
  2. Sometimes it is not possible to remove the other application, which was my case. So I moved JDk installed by me to higher priority level in environment path.

enter image description here

I also removed the path as suggested by @CrazyCoder

Karan
  • 351
  • 4
  • 19
7

This is a setting in IntelliJ IDEA ($JAVA_HOME and language level were set to 1.8):

File > Settings > Build, Execution, Deployment > Gradle > Gradle JVM

Select eg. Project SDK (corretto-1.8) (or any other compatible version).

Then delete the build directory and restart the IDE.

Martin Zeitler
  • 49,224
  • 12
  • 97
  • 156
5

I had similar problem with IntelliJ when tried to run some Groovy scripts. Here is how I solved it.

Go to "Project Structure"-> "Project" -> "Project language level" and select "SDK default". This should use the same SDK for all project modules.

Memin
  • 1,916
  • 19
  • 23
4

I was facing same problem when I installed JRE by Oracle and solved this problem after my research.

I moved the environment path C:\Program Files (x86)\Common Files\Oracle\Java\javapath below H:\Program Files\Java\jdk-13.0.1\bin

Like this:

Path

H:\Program Files\Java\jdk-13.0.1\bin
C:\Program Files (x86)\Common Files\Oracle\Java\javapath

OR

Path

%JAVA_HOME%
%JRE_HOME%

BSMP
  • 3,862
  • 8
  • 31
  • 41
S Kumar
  • 134
  • 1
  • 11
2

how i solve it in Eclipse

  1. go to the properties of the project enter image description here

  2. go to Java compiler enter image description here

  3. change in the Compiler complicated level to java that my project work with (java 11 in my project) you can see that it your java that you work when the last message disappear

  4. Apply enter image description here

Sportac
  • 473
  • 6
  • 20
1

The problem is that you compiled the code with java 13 (class file 57), and the java runtime is set to java 8 (class file 52).

Assuming you have the JRE 13 installed in your local system, you could change your runtime from 52 to 57. That you can do with the plugin Choose Runtime. To install it go to File/Settings/Plugins

enter image description here

Once installed go to Help/Find Action, type "runtime" and select the jre 13 from the dropdown menu.

enter image description here

muilpp
  • 1,123
  • 3
  • 14
  • 32
1

I have run into this issue When I recently upgraded my IntelliJ version to 2020.3. I had to disable a plugin to solve this issue. The name of the plugin is Thrift Support.

Steps to disable the plugin is following:

  1. Open the Preferences of IntelliJ. You can do so by clicking on Command + , in mac.
  2. Navigate to plugins.
  3. Search for the Thrift Support plugin in the search window. Click on the tick box icon to deselect it.
  4. Click on the Apply icon.
  5. See this image for reference Disable_Thrift_support_plugin

For more detail please refer to this link java.lang.UnsupportedClassVersionError 2020.3 version intellij. I found this comment in the above link which has worked for me.

bin zhao commented 31 Dec 2020 08:00 @Lejia Chen @Tobias Schulmann Workflow My IDEA3.X didn't installed Erlang plugin, I disabled Thrift Support 1.4.0 and it worked. Both IDEA 3.0 and 3.1 have the same problem.

Ajay Kr Choudhary
  • 1,028
  • 9
  • 19
0

For me it worked after removing the target folder

Thomas Gotwig
  • 1,156
  • 1
  • 14
  • 13