142

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. My question is 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)

Luan Kevin Ferreira
  • 1,184
  • 2
  • 16
  • 40
jaywayco
  • 5,846
  • 6
  • 25
  • 40
  • 1
    You should check sdk configuration in your project(inside intelliJ) - maybe there you still are using old version - it depends on you configuration – baju Jan 27 '15 at 22:10
  • 1
    Did you get the solution? In my case I switched to using java 7 and everything works. – Willa Apr 28 '16 at 08:43
  • "You are trying to run/reference a class compiled with JDK 8 using a runtime/compiler JRE/JDK 6." Check the accepted answer - https://stackoverflow.com/questions/29906659/java-compiling-error-in-command-prompt-class-file-has-wrong-version-52-0-shoul – prash Sep 29 '17 at 08:14
  • 1
    This is still an issue with intellij 2020.2 trying to switch from java 8 to java 14 – ACV Sep 08 '20 at 21:18
  • 2
    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. – Aditya Patnaik Feb 08 '23 at 11:02

18 Answers18

111

Select "File" -> "Project Structure".

Under "Project Settings" select "Project"

From there you can select the "Project SDK".

Allan Spreys
  • 5,287
  • 5
  • 39
  • 44
  • 25
    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" – Ritesh Feb 24 '21 at 09:47
  • 2
    wondering how this steps order answer the question as is just a matter of use another SDK and not really what the question asked – Carmine Tambascia Aug 20 '21 at 14:13
  • 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 error – dobrivoje Mar 03 '23 at 18:44
36

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

laffuste
  • 16,287
  • 8
  • 84
  • 91
  • 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. – Denis Abakumov May 24 '22 at 17:04
  • 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. – Geralt Dec 06 '22 at 08:39
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.
banan3'14
  • 3,810
  • 3
  • 24
  • 47
  • 6
    "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 opposite – Krzysztof Wolny Sep 17 '15 at 09:23
  • 1
    I corrected the answer according to the @KrzystofWolny's comment. See also https://stackoverflow.com/a/4692743/7769052 – banan3'14 Jan 31 '22 at 12:50
10

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 Path -> libraries -> add JRE system libraries -> add the version of JDK showing in the error as an alternate JRE

enter image description here

AnushaSP07
  • 186
  • 1
  • 3
  • 7
7

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: https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.1-200-B.2

to solve the problem, use the same version of the compiled class file, based on your project structure. in Intellij Idea: File | Project Structure | Project | Project 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 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>
banan3'14
  • 3,810
  • 3
  • 24
  • 47
Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39
5

Have got the same error as in header because of 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 8 and did not recompile with 6. Mixed classes did not compile then. Cleaning project solved the problem. This answer is not strictly relevant to the question, but could be useful for someone.

user3132194
  • 2,381
  • 23
  • 17
  • 2
    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. – santoshM May 30 '19 at 11:59
3

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>
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
Sadman Sakib
  • 360
  • 2
  • 8
1

What does the error signifies is 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.

N.Neupane
  • 281
  • 1
  • 4
  • 17
1

I also faced the same issue, the issue may be because spring boot and java version are not compatible. eg. Spring boot 3 need min 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

0

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

Ritik Raj
  • 47
  • 1
  • 5
0

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

times29
  • 2,782
  • 2
  • 21
  • 40
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.

dobrivoje
  • 848
  • 1
  • 9
  • 18
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"
        }
    }
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
GFPF
  • 959
  • 14
  • 19
-1

If the issue in IntelliJ then do this in terminal(not in IntelliJ terminal)- 1.change the java version to appropriate(in my case using jdk 1.4 as 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

BUGATTI AJAY
  • 759
  • 6
  • 3
  • can you be more specific about 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`? – Kevin Crum Aug 16 '21 at 22:10
-1

i have solved this by changing my POM.XML

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

change 3.0.2 to 2.6.4

-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 add fork="true" wherever i used the javac task and it worked...

praneeth
  • 5
  • 1
-10

In your IntelliJ idea find tools.jar replace it with tools.jar from yout JDK8

ADAM
  • 1
  • 7
    Don't do this. The accepted answer is correct, and all kinds of weirdness could result from this alternative. – JakeRobb Jan 03 '17 at 21:24