Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has been compiled by a more recent version of the Java Runtime (class file version 57.0)

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)
like image 576
nobism Avatar asked Sep 26 '19 23:09

nobism


People also ask

How do I fix Java Lang UnsupportedClassVersionError in Java?

UnsupportedClassVersionError happens because of a higher JDK during compile time and lower JDK during runtime. How can i make above change? Project -> Properties -> Java Compiler Enable project specific settings. Then select Compiler Compliance Level to 1.7, 1.6 or 1.5, build and test your app.

What is the latest version of Java Runtime Environment?

Java Platform, Standard Edition 8Java SE 8u341 is the latest release of Java SE 8 Platform.

How do I download JRE 11?

You can download the JRE free of charge from Oracle. Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html . Under Java Platform, Standard Edition, select either the current release, or click Previous Releases to install an earlier supported version.


5 Answers

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

like image 172
CrazyCoder Avatar answered Oct 19 '22 08:10

CrazyCoder


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.

like image 30
Martin Zeitler Avatar answered Oct 19 '22 07:10

Martin Zeitler


The easiest solution is to change the Java version in your IDE. In Intellij, go to File --> Project Structure and change the Project SDK to the one that is supported by your Java Runtime (in your case it's 52 which corresponds to version 8 or less). Here's a table that shows the mapping between Java SE Version and Major Version:

Java SE Major version
1.0.2 45
1.1 45
1.2 46
1.3 47
1.4 48
5.0 49
6 50
7 51
8 52
9 53
10 54
11 55
12 56
13 57
14 58
15 59
16 60

The table is taken from: https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-4.html

like image 21
Hani Avatar answered Oct 19 '22 07:10

Hani


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

like image 17
Karan Avatar answered Oct 19 '22 07:10

Karan


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.

like image 16
Memin Avatar answered Oct 19 '22 08:10

Memin