Java 为什么在使用 Spring Redis 时 JedisConnection 出现 NoClassDefFound 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33128318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:22:04  来源:igfitidea点击:

Why Getting NoClassDefFound error for JedisConnection when using Spring Redis

javaspringredis

提问by Anand j. Kadhi

Hello when trying to use spring-redis i am getting

你好,当我尝试使用 spring-redis 时,我得到了

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

使用 redis 进行任何连接操作时的异常。我的配置方法是这样的

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

请建议是否有人知道我为什么会收到此异常。

采纳答案by Anand j. Kadhi

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

浪费了差不多一天,发现jar已经在我的类路径上,我进一步调试它,发现当java的反射机制试图找到一个已经存在于“方法列表”中的方法时,它无法找到由于Jedis 版本 (2.7.2) 与 Spring Data Redis (1.5.0.RELEASE) 不兼容,此问题已在此链接中得到解答 ::
Jedis and spring data redis version conflict

回答by abarisone

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

该类org.springframework.data.redis.connection.jedis.JedisConnection不在您的类路径中。请检查您是否有此依赖项,如果缺少,请将其包含在内。

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

缺少的 jar 应该是,给定你的 redis 版本,像这样来自Maven repository redis page,所以在表单中spring-data-redist-(your-version).jar

回答by AnirbanDebnath

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>

回答by Sachin Sridhar

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.

当我们使用 Jedis Configuration 时需要 JedisPoolConfig。在 Spring Boot 2.0 中,spring-boot-starter-data-redis 默认提供了 Lettuce 依赖,而不是 Jedis。要使用 Jedis 配置,请排除 Lettuce 并添加 Jedis,如下所示。

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

回答by Thang Le

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

Redis客户端仍然保留旧包结构的最新版本是: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

从 3.0.x 开始,包结构已更改。如果 spring-data-redis 调用具有旧包结构的 redis 客户端的 Pool 类,则 java.lang.NoClassDefFoundError

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

似乎最新的spring-data-redis:2.1.10.RELEASE仍然使用旧包结构调用redis客户端的Pool类,因此您需要使用redis.clients 2.10.2