Skip to content

springboot-how to solve ClassNotFoundException: redis.clients.jedis.util.Pool when using springboot data redis and jedis?

1. Purpose

In this post, I will demo how to solve the below exception or error when using spring boot data redis and jedis:

Terminal window
> Task :app10:MultiRedisApplication.main()
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-03-20 17:38:25.618 INFO 52410 --- [ main] com.bswen.app10.MultiRedisApplication : Starting MultiRedisApplication using Java 1.8.0_121 on MBP-bswen with PID 52410 (/Users/bswen/private/bw/bswen-github/bswen-springboot23/app10/build/classes/java/main started by bswen in /Users/bswen/private/bw/bswen-github/bswen-springboot23)
2021-03-20 17:38:25.621 INFO 52410 --- [ main] com.bswen.app10.MultiRedisApplication : No active profile set, falling back to default profiles: default
2021-03-20 17:38:26.579 INFO 52410 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-03-20 17:38:26.582 INFO 52410 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-03-20 17:38:26.606 INFO 52410 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7 ms. Found 0 Redis repository interfaces.
2021-03-20 17:38:27.286 INFO 52410 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-20 17:38:27.309 INFO 52410 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-20 17:38:27.309 INFO 52410 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-20 17:38:27.414 INFO 52410 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-20 17:38:27.415 INFO 52410 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1724 ms
2021-03-20 17:38:27.801 WARN 52410 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'multiRedisApplication': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userStringRedisTemplate' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Unsatisfied dependency expressed through method 'userStringRedisTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRedisConnectionFactory' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.RedisConnectionFactory]: Factory method 'userRedisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool
2021-03-20 17:38:27.807 INFO 52410 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2021-03-20 17:38:27.823 INFO 52410 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-20 17:38:27.844 ERROR 52410 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'multiRedisApplication': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userStringRedisTemplate' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Unsatisfied dependency expressed through method 'userStringRedisTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRedisConnectionFactory' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.RedisConnectionFactory]: Factory method 'userRedisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool
Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.util.Pool
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_121]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_121]
... 60 common frames omitted
> Task :app10:MultiRedisApplication.main() FAILED
Execution failed for task ':app10:MultiRedisApplication.main()'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

The core exception is :

Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.util.Pool
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_121]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_121]
... 60 common frames omitted

2. The Environment

  • Jdk 8+
  • Spring boot 2.4+

3. The code

3.1 The project’s dependency

Let’s see the project’s gradle dependencies:

build.gradle
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-data-redis') {
exclude group: 'io.lettuce', module: 'lettuce-core'
}
implementation 'redis.clients:jedis:2.9.0'
}

You can see that we are using spring boot v2.4.2, and we are using spring-boot-starter-redis without lettuce, which is the default connector. we added jedis as redis connection manager, which version is 2.9.0.

When we run the above code, we get this exception:

Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.util.Pool
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_121]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_121]
... 60 common frames omitted

why?

3.2 The reason

This error happens because of the compatibility between spring boot and the jedis versions. By check the compatible jedis version that matches spring boot versions, we can do as follows:

  1. Goto https://github.com/spring-projects/spring-data-redis/blob/2.4.2/pom.xml , if this is not you version ,just click the version selection box, and choose the tag that matches your spring boot version:

    image-20210320210442012

  2. Then you can scroll down the content of the pom.xml, check the jedis version that belongs to this specific spring boot version(see the red arrow):

    image-20210320210648888

  3. Use that jedis version in your build.gradle or maven pom.xml

3.3 The solution

For our spring boot 2.4.2 version, we should define dependencies as follows:

dependencies {
//...
implementation 'redis.clients:jedis:3.3.0'
//....
}

3.4 Re-run the app

Terminal window
20:19:41: Executing task 'MultiRedisApplication.main()'...
> Task :app10:compileJava UP-TO-DATE
> Task :app10:processResources
> Task :app10:classes
> Task :app10:MultiRedisApplication.main()
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-03-20 20:19:44.931 INFO 54173 --- [ main] com.bswen.app10.MultiRedisApplication : Starting MultiRedisApplication using Java 1.8.0_121 on MBP-bswen with PID 54173 (/Users/bswen/private/bw/bswen-github/bswen-springboot23/app10/build/classes/java/main started by bswen in /Users/bswen/private/bw/bswen-github/bswen-springboot23)
2021-03-20 20:19:44.934 INFO 54173 --- [ main] com.bswen.app10.MultiRedisApplication : No active profile set, falling back to default profiles: default
2021-03-20 20:19:46.004 INFO 54173 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-03-20 20:19:46.009 INFO 54173 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-03-20 20:19:46.039 INFO 54173 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 9 ms. Found 0 Redis repository interfaces.
2021-03-20 20:19:46.740 INFO 54173 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-20 20:19:46.765 INFO 54173 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-20 20:19:46.766 INFO 54173 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-03-20 20:19:46.871 INFO 54173 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-20 20:19:46.871 INFO 54173 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1853 ms
2021-03-20 20:19:47.897 INFO 54173 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-20 20:19:48.337 INFO 54173 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-03-20 20:19:48.429 INFO 54173 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-20 20:19:48.447 INFO 54173 --- [ main] com.bswen.app10.MultiRedisApplication : Started MultiRedisApplication in 4.052 seconds (JVM running for 4.593)
2021-03-20 20:19:48.660 INFO 54173 --- [ main] com.bswen.app10.MultiRedisApplication : ==================================================================
2021-03-20 20:19:48.661 INFO 54173 --- [ main] com.bswen.app10.MultiRedisApplication : read the primary redis, key is `hello`, value is world_1
2021-03-20 20:19:48.661 INFO 54173 --- [ main] com.bswen.app10.MultiRedisApplication : read the secondary redis, key is `hello`, value is world_2
2021-03-20 20:19:48.661 INFO 54173 --- [ main] com.bswen.app10.MultiRedisApplication : ==================================================================

It works!

4. Summary

In this post, we demonstrated how to solve the java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool problem when using jedis with springboot, you can see that we should choose the right version of jedis to be compatible with springboot, there is a trick on this job. Thanks for your reading. Regards.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!