Why Getting NoClassDefFound error for JedisConnection when using Spring Redis
Asked Answered
H

8

25

Hello when trying to use spring-redis i am getting

java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnection

exception when doing any connection operation using redis. My config method goes like this

 @Bean
public RedisConnectionFactory jedisConnFactory() {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();

    jedisConnectionFactory.setHostName("XXX.XX.XX.XXX");

    jedisConnectionFactory.setPort(6381);
    jedisConnectionFactory.setUsePool(true);
    jedisConnectionFactory.afterPropertiesSet();
    return jedisConnectionFactory;

Please suggest if anyone knows why i am getting this exception.

Humour answered 14/10, 2015 at 14:36 Comment(4)
You are missing the library (or its not in your class path) that contains the class JedisConnection,.Antonomasia
You dont have it as a dependency, are you using maven?Through
If you are using Maven, then add these 2 guys : pastebin.com/i1yx6fqJThrough
You can find spring data maven version compatible with jedis maven version from bellow link: mvnrepository.com/artifact/org.springframework.boot/…Goby
H
28

After wasting almost one day and finding that the jar is already on my class path, i further debugged it and found that when java's reflection mechanism was trying to find a method which was already present in the "methods list" it was not able to find due to some version conflict between Jedis version (2.7.2) not compatible with Spring Data Redis (1.5.0.RELEASE) , this issue has already been answered in this link ::
Jedis and spring data redis version conflict

Humour answered 23/10, 2015 at 5:15 Comment(2)
How can we get this compatible information? Can you please mention it? documentation or other tools/ plugin?Incestuous
I had to search now as this was 6 years ago when I faced the issueHumour
D
19

latest version that Redis client still keeps old package structure is: 2.10.2.

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.10.2</version>
</dependency>

From 3.0.x, package structure has been changed. if spring-data-redis invokes Pool class of redis client with old package structure, then java.lang.NoClassDefFoundError

seems latest spring-data-redis: 2.1.10.RELEASE still invokes Pool class of redis client with old package structure so you need to use redis.clients 2.10.2

Dow answered 25/8, 2019 at 17:53 Comment(0)
E
17

JedisPoolConfig is needed when we use Jedis Configuration. In Spring Boot 2.0, spring-boot-starter-data-redis gives Lettuce dependency by default instead of Jedis. To use Jedis configuration, exclude Lettuce and add Jedis as following.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>            
</dependency>        
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
Enlistment answered 9/3, 2019 at 9:57 Comment(0)
F
12

In my case I had to remove version tag

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.2.1</version>
</dependency>

to

 <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
 </dependency>
Feints answered 25/4, 2022 at 9:37 Comment(4)
It worked perfectly for me with Spring Boot 2.6.7 version. Thanks a lot!Fluidics
yeah I have version 4.2.3, removing the version downgraded it to 3. something, now it worksYowl
Thanks a lot. It helped me too. I was using spring-boot-starter-parent 2.7.2 . And I was choosing 3.3.0 for jedis. which was wrong. Then I chose latest suggestion by maven and that also did not work. But I let maven choose correct version for jedis by removing version entry in xml . And it worked !!!!!! :))Windswept
Fwiw, this is relevant even if you're not using spring-boot-starter; even if you're just pulling in spring-context.Chiao
F
6

latest version of Redis client solved my issue.

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.6.0</version>
</dependency>
Floatstone answered 16/6, 2021 at 19:50 Comment(0)
F
2

Change to compatible version:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
Foscalina answered 22/12, 2018 at 4:55 Comment(0)
C
2

JedisPoolConfig is needed when we use Jedis Configuration. In Spring Boot 2.0, spring-boot-starter-data-redis gives Lettuce dependency by default instead of Jedis. To use Jedis configuration, exclude Lettuce and add Jedis as following.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <exclusions>
    <exclusion>
     <groupId>io.lettuce</groupId>
     <artifactId>lettuce-core</artifactId>
    </exclusion>
  </exclusions>           
</dependency>       
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
</dependency>
Centonze answered 29/6, 2020 at 12:30 Comment(0)
H
1

The class org.springframework.data.redis.connection.jedis.JedisConnection is not in your classpath. Please check if you have this dependency available and if it's missing include it.

The missing jar should be, given your redis version, like this from Maven repository redis page, so in the form spring-data-redist-(your-version).jar

Horsemint answered 14/10, 2015 at 14:50 Comment(1)
Thanks for your input, The class is already present in my classpath , the issue is with the Jedis version (2.7.2) not compatible with Spring Data Redis version (1.5.0.RELEASE) as mentioned in this link. I am posting this link as an answer so that other people would find it useful.Humour

© 2022 - 2024 — McMap. All rights reserved.