ClassNotFoundException: javax.validation.ValidatorFactory
Asked Answered
P

8

13

I am trying to run a Spring MVC application in my local machine through Tomcat, but I am seeing:

NoClassDefFoundError: javax/validation/ValidatorFactory

I have validation-api-1.0.0.GA.jar in webapp's lib folder. I am using Tomcat 7. Any guidance would be appreciated.

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1629)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser.getValidator(AnnotationDrivenBeanDefinitionParser.java:159)
    at org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser.parse(AnnotationDrivenBeanDefinitionParser.java:106)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1438)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
    ... 23 more
Caused by: java.lang.ClassNotFoundException: javax.validation.ValidatorFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Complete set of jar files I am using is

antlr-2.7.6.jar
antlr-runtime-3.0.jar
aopalliance-1.0.jar
asm-3.3.1.jar
aspectjrt.jar
aspectjweaver-1.6.6.jar
cglib-2.2.2.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-digester-2.0.jar
commons-lang-2.3.jar
commons-logging-1.1.1.jar
displaytag-1.2.jar
dom.jar
dom4j.jar
hibernate-validator-4.2.0.Final.jar
hibernate-validator-annotation-processor-4.2.0.Final.jar
jackson-all-1.8.10.jar
jaxp-api.jar
jaxp-ri.jar
jaxrpc.jar
jdom.jar
jstl-1.2.jar
log4j-1.2.15.jar
ojdbc6.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.aspects-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.web.struts-3.0.5.RELEASE.jar
sax.jar
SecurityFilter.jar
slf4j-api-1.6.4.jar
slf4j-log4j12-1.6.4.jar
tiles-api-2.2.2.jar
tiles-core-2.2.2.jar
tiles-extras-2.2.2.jar
tiles-jsp-2.2.2.jar
tiles-servlet-2.2.2.jar
tiles-servlet-wildcard-2.2.2.jar
tiles-template-2.2.2.jar
validation-api-1.0.0.GA.jar
Peddler answered 29/10, 2014 at 18:15 Comment(3)
You have the necessary jar file you need in hibernate-validator. This jar includes the validation-api. So if you are including validation-api explicitly, you might have dependency conflicts. If not, you might just be having class reloading issues. Try cleaning your tomcat and restarting your IDE. – Voroshilov
Yes, I did that and it worked. Thanks – Peddler
If it works, please accept the correct answer so that others may learn from it too. – Bloodstain
S
10

You have the necessary jar file you need in hibernate-validator. This jar includes the javax.validation.ValidatorFactoryvalidation-api. So if you are including validation-api explicitly, you might have dependency conflicts. If not, you might just be having class reloading issues. Try cleaning your tomcat and restarting your IDE.

Semitropical answered 20/5, 2015 at 15:43 Comment(0)
A
10

add to pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Adelaidaadelaide answered 15/2, 2021 at 17:51 Comment(0)
C
5

😀 I encountered the problem when I launched my tests for the first time.
The problem comes from an incompatibility between the test dependency and the validation dependency.
πŸ“ My pom.xml:

<dependency>
   <groupId>jakarta.validation</groupId>
   <artifactId>jakarta.validation-api</artifactId>
   <version>3.0.2</version>
</dependency>

πŸ˜€ I solved the problem, I replaced jakarta.validation-api for spring-boot-starter-validation.
πŸ“ My pom.xml:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Carven answered 8/7, 2022 at 20:8 Comment(0)
P
0

I had the same problem. For 2 months i was trying to find solution.

There was a error i made. I copied hibernate validator jar and pasted it in java installation directory.

Now i was using maven which also downloaded the validator jar.

There where two jars present and so the error arise. The problem was solved accidentally when i reinstalled java in my system.

Pluck answered 29/6, 2019 at 12:39 Comment(0)
A
0

For me it was a conflict with jakarta.validation version 3.0.1. Rolling it back to 2.0.2 fixed it.

Arthro answered 1/4, 2022 at 11:17 Comment(4)
The OP wasn't using jakarta.validation at all, see his list of dependencies. So there is no way of rolling it back. – Almeria
Well, in reality, I updated a few dependencies, including spring because of that Spring4Shell issue and then I started to receive that error. Reading this post, I tried to add spring-boot-starter-validation and it was already there. I assumed that it is indeed an issue with interference and I've looked for more validation dependencies. I've found jakarta, rolled it back and it worked. Before that I've tried reloading dependencies, clean-install, restart and they didn't work. I understand you rational point of view, but I think you should try to explain reality with it, not deny it. – Arthro
OP asked this question now over 7 years ago - Spring4Shell wasn't even a problem then. OP used Hibernate validator, not Jakarta. And by looking on the picked answer, the problem was a version conflict of specified dependencies, so the problem was probably solved by removing one dependency. Your answer is suggesting to roll back a lib that wasn't there in the problem description - what should be rolled back?? In opposite to the picked answer (remove a dependency) you are effectively suggesting to add one more. – Almeria
You're right. My answer wasn't for him. I'm sure he must have found a solution by now or he moved on. My answer was for people like me who want to learn from his situation in today's world (and tomorrow's, maybe). – Arthro
H
0

Include jakarta.validation-api within your project jar file , and that will force Tomcat to use it. in pom.xml add scope provided as follows:

<dependency>
    <groupId>jakarta.validation</groupId>
    <artifactId>jakarta.validation-api</artifactId>
    <version>3.0.2</version>
    <scope>provided</scope>    
</dependency>

good luck.

Havard answered 28/9, 2022 at 7:15 Comment(0)
T
0

I faced this error upgrading a project to Spring boot 3.1.0, which does not use javax anymore but Jakarta.

Solution was adding hibernate Validator dependency on your root pom.

<dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>8.0.0.Final</version>
</dependency>

Original Answer is from Manu Chahar

Toxoplasmosis answered 6/8, 2023 at 19:8 Comment(0)
E
0

add this to your build.gradle:

implementation 'javax.validation:validation-api:2.0.1.Final'
Esculent answered 4/10, 2023 at 17:10 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.