Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Getting NoClassDefFound error for JedisConnection when using Spring Redis

Tags:

java

spring

redis

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.

like image 323
Anand Kadhi Avatar asked Oct 14 '15 14:10

Anand Kadhi


3 Answers

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

like image 152
Anand Kadhi Avatar answered Oct 19 '22 16:10

Anand Kadhi


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

like image 16
Thang Le Avatar answered Oct 19 '22 14:10

Thang Le


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>
like image 15
Sachin Sridhar Avatar answered Oct 19 '22 14:10

Sachin Sridhar