Migrating to java 17 mandatory module system, where do I find module javax.validation? – Java

by
Liam Thompson
java java-17 jsr380 module-info validation

Quick Fix: In Java 17, you can migrate to the mandatory module system by either using jakarta.validation or java.validation module. Here are the steps:

  1. Add the required module in your module-info.java file:

    requires jakarta.validation;
    // or
    requires java.validation;
    
  2. Add the latest Jakarta Validation API dependency to your project:

    <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
        <version>3.0.2</version>
    </dependency>
    
  3. Update your code to use the new packages. For example, instead of javax.validation.Validator, use jakarta.validation.Validator.

Refer to the documentation for more information.

The Problem:

While migrating a multi-module project to Java 17, an issue arises when trying to provide module dependencies in module-info.java files. Specifically, the javax.validation module is not found, despite adding various "requires" lines in module-info.java. The project uses a maven dependency for javax.validation, but it’s unclear how to obtain the corresponding Java module for this dependency.

The Solutions:

Solution 1: Use jakarta.validation or java.validation

javax.validation is not available as a Java module. You can use either `jakarta.validation` or `java.validation` instead. The latest version of the Jakarta Validation API is 3.0.2. You can add the following dependency to your pom.xml file:

&lt;dependency&gt;
    &lt;groupId&gt;jakarta.validation&lt;/groupId&gt;
    &lt;artifactId&gt;jakarta.validation-api&lt;/artifactId&gt;
    &lt;version&gt;3.0.2&lt;/version&gt;
&lt;/dependency&gt;

You can also use the java.validation module. This module is part of the Java Development Kit (JDK) and provides a limited subset of the Jakarta Validation API. To use this module, you can add the following line to your module-info.java file:

requires java.validation;

You can find the Jakarta Validation specification here. 3.0 is current, while 3.1 is under development. Hibernate Validator is the sole implementation for 3.0 & 3.1.

Q&A

Where do I get the java module for the javax.validation dependency?

javax.validation is not available as a Java module.

What are the options to use instead of javax.validation?

java.validation or jakarta.validation.

What is the latest version of Jakarta Validation API?

3.0.2.

Video Explanation:

The following video, titled "Migrating to Java 9 Modules with ModiTect by Gunnar Morling ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

runtime images containing just the modules an application needs, the advantages of the module system are manyfold. How to migrate existing ...