Compile error "Class file has wrong version 52.0, should be 50.0"
Asked Answered
D

21

159

I'm trying to compile my project in IntelliJ IDEA. I'm using a class in an external JAR file and on compilation receiving the following error.

Class file has wrong version 52.0, should be 50.0

I understand that it's saying the JAR file was compiled with a newer Java version than that which IntelliJ is using. How do I make them compatible?

I have updated the Java on my Mac to 1.8 and when I run java -version or javac -version it confirms this.

What am I missing? (Apart from Java development experience)

Danu answered 27/1, 2015 at 22:5 Comment(5)
You should check sdk configuration in your project(inside intelliJ) - maybe there you still are using old version - it depends on you configurationDecorator
Did you get the solution? In my case I switched to using java 7 and everything works.Chromaticity
"You are trying to run/reference a class compiled with JDK 8 using a runtime/compiler JRE/JDK 6." Check the accepted answer - #29907159Snuff
This is still an issue with intellij 2020.2 trying to switch from java 8 to java 14Darky
I got this error when I was trying to run Spring boot 3.X on java 8. Spring Boot 3.X is compatible only with java 17 and above. Hope this finds someone in need.Selfsuggestion
F
119

Select "File" -> "Project Structure".

Under "Project Settings" select "Project"

From there you can select the "Project SDK".

Frontolysis answered 27/1, 2015 at 22:10 Comment(3)
If using Gradle, along with above settings, please also select appropriate Gradle JVM settings. For IntelliJ IDE, its under tab: "IntelliJ IDEA" -> "Build, Execution, Deployment" -> Gradle, and update the Gradle JVM under "Gradle Projects"Silvasilvain
wondering how this steps order answer the question as is just a matter of use another SDK and not really what the question askedGuanidine
Or, if you can't change SDK like in my case, then lower maven dependency of the library causing this error. I've had SDK 1.8_191, and I had to use TestNG 6.14.3, as versions 7.x caused this errorAntimere
T
40

If the error comes from an external dependency (Maven/Gradle), the version you imported requires a newer JDK. (e.g., you imported Caffeine 3.0.x which requires Java 11, but you are using Java 8.)

Solution: downgrade the dependency to the latest version compatible with your JDK.

Java version numbers can be found in Java class file wiki page (byte offset 6-7).

Tletski answered 18/6, 2021 at 9:37 Comment(2)
Thanks! This explains why TestNG 7.6.0+ can't be used with Java 8. I downgraded the TestNG dependency to 7.5 and it worked.Johore
I had a similar problem "class file has wrong version 61.0, should be 52.0" for using the fundamental org.springframework.context.annotation.ComponentScan class! For me changing the JDK/JRE version didn't solve the problem. Then I downgraded spring-webmvc dependency version from 6.0.2 to 5.2.6.RELEASE and it eventually worked.Ichor
L
33

It means your Java runtime version is 1.6, but your compiler version (javac) is 1.8. To simply solve it, just advance your JVM version to 1.8.

But if you don't want to change the Java runtime version, then do the following steps:

  1. JAVA_HOME= "your JDK v1.8 folder path", to make sure JDK is also v1.8 and use java -version and javac -version again to ensure it
  2. Make sure IntelliJ's compiler mode is set to compliant with v1.6. But I have tried that. It didn't solve my problem.
Latterday answered 17/2, 2015 at 8:40 Comment(3)
"it means your java runtime version is 1.8, but your compiler version (javac) is 1.6. To simply solve it, just retreat the java version from 1.8 to 1.6." - rather oppositeDealfish
I corrected the answer according to the @KrzystofWolny's comment. See also https://mcmap.net/q/152643/-is-jdk-quot-upward-quot-or-quot-backward-quot-compatibleCamorra
Re "It didn't solve my problem.": What is that supposed to mean? Why is it in an answer? Doesn't the last part of the answer work? Why is it included, then?Spectroscopy
B
18

Check the version of Java it is showing in the error:

  • 45 = Java 1.1
  • 46 = Java 1.2
  • 47 = Java 1.3
  • 48 = Java 1.4
  • 49 = Java 5
  • 50 = Java 6
  • 51 = Java 7
  • 52 = Java 8
  • 53 = Java 9
  • 54 = Java 10
  • 55 = Java 11
  • 56 = Java 12
  • 57 = Java 13

Install/download that JDK in program files/Java folder.

In Eclipse or STS, right click on project → Build PathLibraries → add JRE system libraries → add the version of JDK showing in the error as an alternate JRE.

Enter image description here

Beliabelial answered 23/1, 2023 at 17:0 Comment(0)
U
9

Generally this kind of error happens when you try run a .class file which is compiled with another version of Java.

In your case, it is compiled with Java 8 (major version -> 52) and you want to run it with Java 6 (major version -> 50).

Here is a list of major versions of Java and SE version: Class file format major versions

To solve the problem, use the same version of the compiled class file, based on your project structure. In IntelliJ IDEA: Menu FileProject StructureProjectProject SDK

In case of using builder tools like Maven, this configuration also should be changed in the corresponding settings. Here is an example of how it is done in a POM file:

<properties>
    <java.version>11</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
Unipersonal answered 5/1, 2022 at 9:10 Comment(0)
C
5

I have got the same error as in the header because of a failed attempt to compile my project with Java 8 and then reattempting to compile with Java 6.

Some classes where compiled at the first attempt with Java 8 and did not recompile with Java 6. Mixed classes did not compile then. Cleaning the project solved the problem.

This answer is not strictly relevant to the question, but it could be useful for someone.

Cumuliform answered 14/9, 2015 at 7:55 Comment(1)
Cleaning project did not do the job for me. I just removed the classes, which were throwing the error and did a clean build to get them compiled by 1.6. This resolved my issue.Weintrob
O
4

I am using JDK 1.8.0_131 and For my case, the version code <version>2.7.5</version> in pom.xml and module -> language level 8 solved my issue.

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/>
    </parent>
Oaxaca answered 28/2, 2023 at 4:44 Comment(0)
M
2

If you are using javac to compile, and you get this error, then remove all the .class files

rm *.class     # On Unix-based systems

and recompile.

javac fileName.java
Mcatee answered 12/11, 2020 at 2:22 Comment(0)
E
1

The error signifies that you may have the dependencies that are compiled in Java 11, but you are using Java 8, so there are two options left. You can either migrate to Java 11 or ask the library developer to provide you with a Java 8 build.

Earpiercing answered 27/1, 2022 at 2:27 Comment(1)
the versions you mentioned are wrong. It should be 8 (52.0) and 6 (50.0) instead of 11 (55.0) and 8.Camorra
T
1

I also faced the same issue. The issue may be because Spring Boot and Java version are not compatible. For example, Spring Boot 3 need minimum JDK 17, so if you are using Java 8 then you have to downgrade your Spring Boot version to 2.*

Does Spring 3.0.x support Java 8? In Spring Starter I found sourceCompatibility = '17' default for 3.0.x and above

Tice answered 29/5, 2023 at 15:59 Comment(1)
But for Spring boot 3, I have installed java 8 and its working fine in Eclipse without any problem but not in IntelliJ why?Vibrator
R
0

Class version 52 is Java 8. You need Java 11.

Raillery answered 17/10, 2022 at 5:21 Comment(0)
E
0

I installed a new JDK version, but I forgot to set the JAVA_HOME environment variable to the new JDK. Setting it to the correct version helped!

Estabrook answered 7/11, 2022 at 9:22 Comment(0)
A
0

I couldn't change an SDK version, so I had to lower maven dependency of the library causing this error. I've had SDK 1.8_191, and I had to use TestNG 6.14.3, as versions 7.x wouldn't work.

Antimere answered 3/3, 2023 at 18:46 Comment(0)
E
0

Project's build.gradle file:

allprojects {    tasks.withType(org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile).configureEach {
        kotlinOptions {
            jvmTarget = "1.8"
            apiVersion = "1.8"
            languageVersion = "1.8"
        }
    }
}
Enneastyle answered 25/4, 2023 at 6:53 Comment(0)
M
0

If you are creating your own JAR file, make sure that the application in which you are importing the JAR file has 'id' in the plugins section as 'application'.

plugins {
    id 'application'
}

If both JAR and application has 'id' as 'java', we might see a similar error.

Medarda answered 14/11, 2023 at 8:49 Comment(0)
N
0
For Java 8 spring boot compactible version is 2.2.6.RELEASE

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath /> 
    </parent>
    
Nottingham answered 13/3 at 2:44 Comment(0)
V
0

This nonsense still persists in 2024 in an IntelliJ kotlin project. I tried everything in the already given answers, but there's now a new way of getting the error:

Despite having everything set correctly in both "Project Structure" and "Build, Execution, Deployment" -> "Gradle" -> "Gradle JVM", my project still compiled to a newer java version than my target system could handle. The solution for me after hours upon hours?

Change your build.gradle.kts to use the correct toolchain, in my case

kotlin {
   jvmToolchain(17) // I can't run 18 atm.
}
Vange answered 25/3 at 15:45 Comment(0)
D
-1

If the issue is in IntelliJ, then do this in the terminal (not in the IntelliJ terminal):

  1. change the Java version to an appropriate one (in my case, using JDK 1.4 as the issue was class file has wrong version 55.0, should be 52.0)
  2. java -Dspring.profiles.active=test -jar build/libs/<jarname>.jar
    Run this
Diverting answered 22/7, 2021 at 5:42 Comment(1)
can you be more specific about <jarfile> argument? If my error is bad class file: /usr/share/openjfx/lib/javafx.graphics.jar!/javafx/application/Platform.class class file has wrong version 54.0, should be 52.0 Please remove or make sure it appears in the correct subdirectory of the classpath. then is the jarfile graphics.jar?Gaylagayle
T
-1

I have solved this by changing my pom.xml file:

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>

Change 3.0.2 to 2.6.4.

Turkestan answered 15/2, 2023 at 10:37 Comment(0)
I
-2

I faced the same problem, "Class file has wrong version 52.0, should be 50.0" when running Java through Ant...

All I did was adding fork="true" wherever I used the javac task, and it worked...

Iridotomy answered 19/5, 2015 at 13:17 Comment(1)
adding fork = true in configuration didn't work for me.Cambrel
G
-10

In your IntelliJ IDEA, find file tools.jar. Replace it with tools.jar from your JDK 8.

Gharry answered 4/10, 2015 at 10:32 Comment(1)
Don't do this. The accepted answer is correct, and all kinds of weirdness could result from this alternative.Langill

© 2022 - 2024 — McMap. All rights reserved.