Maven using wrong version of javax.validation

21,428

Solution 1

Try adding this dependency to your pom.xml

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

Solution 2

The situation is provoked by having javaee-web-api 7.0 as dependency:

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-web-api</artifactId>
  <version>7.0</version>
  <scope>provided</scope>
</dependency>

This dependency provided by your servlet or application container at runtime is giving you an older api for javax.validation.

You have two things to try:

Change the version of javaee-web-api to 8.0

This is the fastest solution if you are deploying your web app to a servlet or application container that supports Java EE 8.0 (for example: it seems to work in Tomcat 8.5.X but reading the documentation, only Tomcat 9 provides support for Java EE 8).

Keep javaee-web-api as 7.0 and downgrade hibernate-validator, validation-api dependencies:

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.1.0.Final</version>
</dependency>

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

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator-annotation-processor</artifactId>
  <version>5.4.2.Final</version>
</dependency>    

You have to change also some imports such as @Email or @NotEmpty from javax.validation package to org.hibernate.validator.constraints (those annotations are Deprecated in 6.0.X hibernate-validator versions because they're included inside javax.validation api).

Solution 3

Do mvn dependency:tree , May you have another dependencies that use hibernate-annotations, and conflict with your spring version, also you can find them and exclude from class path, as below:

 <exclusions>
    <exclusion>
       <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
    </exclusion>
  </exclusions>

I hopefully help you.

Solution 4

Had the same problem with Springboot 2.1.4.RELEASE and some GeoTools dependencies.

Validation-api was specified from javax.validation and javaee-api jars.

Put them as dependencies like following:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
</dependency>

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

Solution 5

Had the same issue as jekho, but solved it with:

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-referencing</artifactId>
    <version>22.3</version>
    <exclusions>
        <exclusion>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

I found which dependency was using javaee-api by using maven's dependency:tree on my project which points out which packages are being used by which dependency.

Share:
21,428
Connor Butch
Author by

Connor Butch

Updated on July 21, 2022

Comments

  • Connor Butch
    Connor Butch almost 2 years

    I have a dynamic web project I have been working on that uses hibernate as a jpa provider. Up until the last week, I could insert, update, query, and delete from my databases using hibernate. Recently, I began working on validation, and brought in a lot of different maven dependencies. In the course of doing this, I somehow have ended up with my project using an older, deprecated version of javax.validation jar, which throws a no such method exception. I have included the relevant lines of the stack trace below:

    Caused by: java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName()Ljava/lang/String;
    at org.hibernate.validator.internal.xml.ValidationBootstrapParameters.<init>(ValidationBootstrapParameters.java:63)
    

    However, in my pom.xml I have the following dependency (which, when examined via javap and through eclipse, it has the method getClockProviderClassName() in the specified class):

    <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.0.Final</version>
        </dependency>   
    

    I put the following exclusion tag in my pom in multiple dependencies to try and ensure that it does not pull this jar from other sources:

                <exclusion>
                    <artifactId>validation-api</artifactId>
                    <groupId>javax.validation</groupId>
                </exclusion>
    

    I have also simplified my build path, so there is three elements in it: one properties folder that contains only my log4j configuration, the JRE System library (version 1.8) and maven dependencies. Is there any way to tell where the deprecated javax.validation jar is coming from? Can I force the program to use version 2.0.0 I bring in with maven? Thank you for your help.

    Additional Information:
    full stack trace

    persistence.xml source

    working datasource properties

    Java Build Path

    Line where root exception occurs (in my code)

    look inside the included jar to see it contains the method

    using javap to examine artifact in maven (m2) directory

    dependency overview from maven

    Dependencies from pom.xml

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.2.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.2.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>jboss-logging</artifactId>
                    <groupId>org.jboss.logging</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>validation-api</artifactId>
                    <groupId>javax.validation</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <!-- https://stackoverflow.com/questions/20859379/cannot-import-javax-ejb-packages -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jboss.logging/jboss-logging -->
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.3.0.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.3</version>
        </dependency>
        <!-- ********************************************************************************************************** -->
        <!-- THE FOLLOWING ARE SEPERATE FROM HIBERNATE THAT IS USED TO CONNECT 
            TO DB, THESE ARE FOR USE WITH VALIDATION -->
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.7.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>validation-api</artifactId>
                    <groupId>javax.validation</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator-annotation-processor -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator-annotation-processor</artifactId>
            <version>6.0.7.Final</version>
                        <exclusions>
                <exclusion>
                    <artifactId>validation-api</artifactId>
                    <groupId>javax.validation</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator-cdi</artifactId>
            <version>6.0.7.Final</version>
                        <exclusions>
                <exclusion>
                    <artifactId>validation-api</artifactId>
                    <groupId>javax.validation</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.0.Final</version>
        </dependency>       
        <!-- ********************************************************************************************************** -->
        <!-- API and reference implementation of expression language {a.b} in html 
            code -->
        <!-- https://mvnrepository.com/artifact/javax.el/javax.el-api -->
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.web/javax.el -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.6</version>
        </dependency>
    </dependencies>