78

I'm working with Maven with Java 11 and Maven on IntelliJ IDEA. I'm trying create a JsonTset class as follows:

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.nio.file.Paths;

@JsonTest
public class JacksonInjectExample {

    private static final String filePath = "test.json";

    private static final String JSON = String.format(String.valueOf(Paths.get(filePath)));

    @Autowired
    private JacksonTester<OHStepTrackingNotification> jacksonTester;

    @Configuration
    public static class TestConfiguration {...}

    @BeforeClass
    static public void setSystemProperties() throws IOException {...}

    @Test
    public void test() throws IOException {...}
}

But the build is failing on the below imports:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

It fails with the error:

java: cannot access org.springframework.beans.factory.annotation.Autowired
  bad class file: /C:/Users/<username>/.m2/repository/org/springframework/spring-beans/6.0.2/spring-beans-6.0.2.jar!/org/springframework/beans/factory/annotation/Autowired.class
    **class file has wrong version 61.0, should be 55.0**
    Please remove or make sure it appears in the correct subdirectory of the classpath.

I understand this problem is related to Java versions, but I can't find any working solution.

Using Java 11 which is set in JAVA_HOME.

What I have tried:

  1. Update JDK to Java 11.
  2. java -version gives: openjdk version "11.0.17" 2022-10-18 LTS
  3. Update the project in IntelliJ to work with Java 11.
  4. Remove .idea folder.
  5. Invalidate caches

How can I fix the confusing version?

6
  • 10
    Spring Framework versions 6.0 and later require Java 17. If your code must work with Java 11 you cannot use Spring Version 6. Commented Dec 1, 2022 at 21:56
  • Is there any way to use Spring Framework in lower java versions?
    – R H
    Commented Dec 1, 2022 at 22:04
  • Use Spring 5 or older.
    – CrazyCoder
    Commented Dec 1, 2022 at 22:05
  • 3
    Spring Framework 5.3 is the latest version that can be used with Java 8 and Java 11. The (at the moment) latest release is 5.3.24: docs.spring.io/spring-framework/docs/5.3.24/reference/html/…. Similar for Spring Boot: Spring Boot 3 requires Java 17. If you need to use Java 11 the latest usable version is Spring Boot 2.7 (2.7.5) Commented Dec 1, 2022 at 22:48
  • 3
    no images of plain text please
    – kleopatra
    Commented Mar 29, 2023 at 15:53

11 Answers 11

125

You're using Spring Framework 6, and since Spring 6, the minimum supported Java version is Java 17 (the class version 61 is the class version of Java 17).

As documented in the Spring Framework Overview for Spring 6:

As of Spring Framework 6.0, Spring requires Java 17+.

So, if you want to use Spring 6, you need to upgrade to Java 17. If you want to continue using Java 11, you need to downgrade to Spring 5.3.

Given Spring Boot 3 is built on Spring Framework 6, the same applies to Spring Boot 3: you need to upgrade to Java 17, or you need to downgrade to Spring Boot 2.7.

1
  • 3
    Same goes for Spring boot 3.x.x > it requires Java 17+
    – Naor Bar
    Commented Jul 17, 2023 at 12:12
22

To use version 11, if your project was created with version 17, change in pom.xml adj reference image:

Enter image description here

5
  • 5
    The mysql line doesn't mean anything here, also show the needed change in code blocks, rather than a screenshot. Commented Feb 5, 2023 at 13:04
  • when creating a project a spring version 3.0 project onwards and with java 17.. the mysql driver dependency is <artifactId>mysql-connector-j</artifactId> so you should change the dependency so that you don't get an error to a previous version.. Commented Feb 5, 2023 at 23:05
  • Latest version of spring-boot-starter-parent was 2.7.11 as of this writing: mvnrepository.com/artifact/org.springframework.boot/…
    – Champ
    Commented May 15, 2023 at 0:56
  • It has solved my issue when I degraded the project from java-17 and Spring-boot:3.1.0 to java-11 and Spring-boot: 2.7.0. Thanks. Commented May 27, 2023 at 9:52
  • Please provide a text version. Please review Why not upload images of code/errors when asking a question? (e.g., "Images should only be used to illustrate problems that can't be made clear in any other way, such as to provide screenshots of a user interface.") and do the right thing (it covers answers as well). Thanks in advance. Commented Jan 6 at 12:09
6

Maybe it’s because of inconsistency between the Java version and the Spring or Spring Boot version. Check your version of Spring. Here is a snippet from the Spring site:

We are planning to release Spring Boot 3.0 in November 2022. This next major revision will be based on Spring Framework 6.0 and will require Java 17 or above. It will also be the first version of Spring Boot that makes use of Jakarta EE 9 APIs (jakarta.) instead of EE 8 (javax.).

From Preparing for Spring Boot 3.0.

4

I am using Gradle and IntelliJ IDEA. So I did the following updates and it solved my problem.

  1. PreferencesBuild,Execution,DeploymentCompilerJava Compiler (updated as 17)

    Enter image description here

  2. Preferences* → Build,Execution,DeploymentBuild ToolsGradle (updated Gradle JVM to Project SDK 17)

    Enter image description here

3

As of this writing, IntelliJ IDEA automatically uses Spring Boot 3.0 when creating a new Spring Boot project—even if you specify that it should use an incompatible Java version (i.e., earlier than Java 17). To use a 2.x version of Spring Boot, you will need to use the Spring Initializer:

https://start.spring.io/

You can then extract and load this into IntelliJ.

1

If running from Eclipse (e.g. Maven commands), you need to be using the right JDK version for Run As -> Maven. Ensure that your JRE version is selected appropriately under the JRE tab:

enter image description here

0

I had the same error, and I was able to resolve it by redirecting to my .m2 directory present in C:\Users\Name.m2 and delete the repository folder.

Then open the project back in IntelliJ, right click on project → Maven, and then → Download sources and documentation.

0

Make sure you have the correct parent version as well!

I had the v3.0.5 parent version, and I still got this error. After I downgraded to v2.7.1, the error disappeared!

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

We had to work with Java 11 and got the same problem. It was solved by removing the "version" tag from:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
</dependency>
0

For me this worked. JAVA_HOME on your machine could still be pointing to Java 11, even if you changed it to Java 17 in IntelliJ.

Here is what I followed:

  1. Open a terminal and type in: java --version. If this gives you "java 11", that means you need to change the JAVA_HOME path to use Java 17 and not Java 11.

  2. Go to IntelliJ: menu FileProject Structure (copy the JDK home path for Java 17)

    Enter image description here

  3. in the terminal: export JAVA_HOME="<JDK home path>"

  4. in the terminal: echo $JAVA_HOME to verify if its set to Java 17

  5. it should work for you now :)

1
  • Re "gives you "java 11"": It may give something like "openjdk 11.0.21" instead. Commented Jan 6 at 12:26
-2

In my case, I deleted the path deployment, and it worked.

1
  • 2
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 21, 2023 at 14:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.